JavaScript Objects-Learning Notes

Source: Internet
Author: User
Tags object serialization hasownproperty

JavaScript authoritative Guide To learn notes, Forbidden reprint!

5. Objects

The properties of an object are unordered, and each property has a string key and value,

Tags for data properties: Writable, enumerable, configurable, value

tags for accessor properties: enumerable, Configurable, get, set. Get indicates that the property is readable, and set indicates that the property is writable. An underscore in front of the property indicates that the property is accessible only through the object method Get/set, which is the accessor property.

(1) Create an object

1) literal var obj={x:1,y:2};

2)new: Var obj=new foo (); (The proto of obj points to the foo.prototype of the constructor foo)

For example, function foo () {};

var obj=new foo ();

typeof obj; "Object"

typeof Obj.tostring; "Function" (the ToString method is the method on the Object.prototype on the obj prototype chain)

Note: if (obj.z;//undefined) does not represent that the Obj object and its prototype chain have no attribute Z, there may be attribute Z but the value is undefined.

3)Object.create(prototype)

For example, Var obj= object.create ({x:1}); The prototype of obj at this time {x:1}.

typeof Obj.tostring; "Function"

Note: But not every object has Object.prototype on the prototype chain, so not every object has a tostring method, such as Var obj= object.create (NULL);  At this point null is the prototype of obj and the top of the prototype chain, so TypeOf Obj.tostring; Undefined

(2) attribute operation

~ ~ Property Read:

For example, Var obj={x:1,y:2};

obj.x; 1

obj["X"]; 1 (not used)

var p; for (P in obj)  {Console.log (obj[p]); }

1

2

(This method reads the properties on the OBJ prototype chain, and the order of the attributes is indeterminate.) You can use hasOwnProperty to filter out the properties on the prototype chain. )

Read properties are found on the prototype chain.

~ ~ Detects if a property exists:

obj.hasownproperty(property name): Determines whether the object obj itself has this property, and returns True or false.

obj.propertyisenumerable(property name): Determines whether this property of the object obj can be enumerated. Properties on the prototype chain are generally not enumerable.

(3) Get/set method and prototype chain

For example, var obj = {};

Object.defineproperty (obj, ' x ', {value:1,writable:false,configurable:false,enumerable:false});

var obj1 = object.create (obj);

obj1.x = 10;

Console.log (obj1.x); 1

When assigning a value to an object's properties (obj1.x = 10;):

If there is an X attribute on the OBJ1, modify its value.

If there is no X attribute on the obj1, it is looked up to its prototype chain, and if there is no X attribute on the prototype chain, then the x attribute is added to the Obj1 object, and if there is an X attribute on the prototype chain, there are three cases:

A, the X attribute writable:true on the prototype chain, the X attribute is added on the obj1, and the attributes on the prototype chain are masked.

B, the prototype chain x attribute Writable:false, this assignment statement will be ignored, if it is in strict mode will be error, so the above obj1.x is still 1,

C, the prototype chain X has a Set/get method, or take the Set/get method on the prototype chain, instead of adding a new X attribute to obj1. At this point, if you need to add an X attribute to obj1, use Object.defineproperty (obj1, ' Z ', {value:10,configurable:true});

mark!

(4) Attribute tags

Object.getownpeopertydescriptor (object name, ' property name '): Returns an object that contains all the tags for this property.

Object.defineproperty (Object name, property name, property's label) (the property defined by this method, the default value of configurable, enumerable, writable is false)

For example, Var b={};

Object. DefineProperty (b, ' B ', {configurable:false,writable:true});

Object.getownpropertydescriptor (b, ' B ');//object {value:undefined, writable:true, Enumerable:false, configurable: False

For example, Var person={};

Object. defineproperties (person,{

Salary:{value:50000,writable:true},

promote:{

Set:function (level) {

this.salary*=1+level*0.1;

}

}//promote End

});

Object {salary:50000}

salary:50000

Set Promote:function (Level)

__proto__:object

person.promote=2; Call the set method to pass a value to the function's parameter level

Person.salary; 6000

Object.keys (object): Returns an array that contains all the enumerable property names on the object.

Note: if Writable:false,configurable:true, You can modify the property value in a disguised way by object.defineproperties the value label of the property, and of course the writable tag can be modified to true.

(5) Object label

_Proto_: Prototypes

class: Object type

Object.prototype.toString.call () detects the data type, including the type of the object.

Note: Object.prototype.toString.call (new number (1)); "[Object number]"

Object.prototype.toString.call (1); "[Object number]"

Extensible: Whether a property on an object can be added

object.isextensible (obj); Determine if an object is extensible

object.preventextensions (obj); Make an object non-extensible

Object.seal (obj): Causes property Configurable on an object to become false

object.issealed (obj) judgment

Object.freeze (obj): Causes properties configurable and writable on an object to become false

Object.isfrozen (obj) judgment

These do not affect the object's prototype chain.

(6) Object serialization JSON

The process of converting an object to a sequence of bytes (data is transmitted over a network in the form of a binary sequence)

json.stringify (): Converts JavaScript values to JSON strings,

If the property value is undefined, it will not appear in the result string, if the property value is Nan or infinity, it is converted to null, and if date is converted to UTC time format,

For example, Var obj={x:1, Y:true, z:[1,2,3], Nullval:null, val:undefined, A:nan, B:infinity,c:new Date ()};

Json.stringify (obj);

"{" X ": 1," Y ": true," Z ": [Nullval]," a ": null," A ": null," B ": null," C ":" 2017-05-20t11:48:28.044z "}"

Serializing a custom

var obj={x:1, Y:2, o:{

O1:1, O2:2, ToJSON:function () {return this.o1+this.o2;}

}

};

Json.stringify (obj);

"{" X ": 1," Y ": 2," O ": 3}"

Json.parse (): Converts a JSON string to an object.

For example, Var str= ' {"x": 1, "Y": 2} ';

Json.parse (str);

Object {x:1, y:2} (single quote is written outside {}, each property name must be double quotes, otherwise an exception will be thrown uncaught syntaxerror:unexpected identifier)

(7) ValueOf, tostring Method

For example, Var obj={x:1, y:2};

obj.toString=function () {return this.x+this.y;}

+obj; 3 (+ Plus operator forces the object to be converted to the base type)

obj.valueOf=function () {return this.x+this.y+100;}

+obj; 103

"Result" +obj; "Result103"

When both the valueof and ToString methods are present, the valueof is found first, if the valueof return value is the base type, whichever is the end, if the valueof return value is the object and then find ToString, the ToString return value is an error.

JavaScript Objects-Learning Notes

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.