Introduction to JavaScript Objects _javascript tips

Source: Internet
Author: User

In JavaScript, all other values are objects except number, string, Boolean, NULL, and undefined. objects can be declared directly by literal amounts, or they can be created by using the new operator. Unlike the Java language, the property in a JavaScript object can be added or deleted dynamically, and the property in the object can also be an empty string:


Copy Code code as follows:

Properties in object can be added/deleted dynamically
var o = {x:1, y:2};
Console.log (o);//object {x=1, y=2}
Delete o.y;
O.z = 3;
Console.log (o);//object {x=1, z=3}

Empty string is allowed as Object
var O2 = {"": the, "P": 99};
Console.log (O2);//object {=88, p=99}

For constructor function, the "new" Operation returns an object.
function Computer (x, y) {
this.x = x;
This.y = y;
}
var c = new Computer (126, 163);
Console.log (c);//computer {x=126, y=163}
var C2 = new Computer (126);//missing parameter value would be "undefined"
Console.log (C2);//computer {x=126, y=undefined}
C.z = 66;
Console.log (c);//computer {x=126, y=163, z=66}
Delete c.y;
Console.log (c);//computer {x=126, z=66}

If you use the new operator to create a new object, the role function is not a constructor of a class, but just a normal function, then JavaScript will return an empty object after executing the function:

Copy Code code as follows:

For pure function, "new" Operation returns a empty object.
function compute (x) {
Console.log ("Execute function compute");
return x*2;
}
var a = new compute ();
Console.log (a);//compute {}

object property

The object in JavaScript has the following 3 properties:

1.prototype. A reference that points to the prototype object. The property in the prototype object can be inherited by object.
2.class. A string that represents the class name of object.
3.extensible. Boolean value that indicates whether the property is allowed to be added dynamically in object. This property is valid only in ECMAScript 5.

Property Properties

The property in object also has 3 properties:

1.writable. Whether the property is writable.
2.enumerable. Whether the property is enumerated when the For/in statement is used.
3.configurable. Whether the property's properties can be modified and whether it can be deleted.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.