Javascript notes of objects

1. The structure of the object:

Object contains a series of attributes that are unordered. Each attribute has a key and a corresponding string value. There are two ways to access the object, as shown below:

 

var obj = {};
obj[1] = 1;
obj['1'] = 2;
obj; // Object {1: 2}

var obj = {};
obj.y = 2;
obj.x = 1; 
obj ; //{y: 2, x: 1}

2. Create the way the object

2.1 to create objects by literal way, as shown below, then access an object when you can access by way of obj2.z

There obj2 = { 
    x: 1 , 
    y: 2 , 
    then: { 
        z: 3 , 
        n = 4 
    } 
};

2.2. By creating new / object prototype chain manner to create objects by way of inheritance, the following code, corresponding to the parent class foo, equivalent subclasses obj, z foo defined attributes, when accessing subclass when a property is not defined subclasses, looks up to find out if any of the properties of the parent class prototype property, it is possible to look for the property, but the property does not belong to the subclass.

function foo(){}
foo.prototype.z = 3;

//创建对象
var obj =new foo(); obj.y = 2; obj.x = 1; obj.x; // 1 obj.y; // 2 obj.z; // 3 typeof obj.toString; // ‘function' 'z' in obj; // true obj.hasOwnProperty('z'); // false

 

 

 But when a subclass to redefine the prototype in the corresponding attribute, delete the property when he can only remove their own defined properties, the properties can not delete the parent class definition

obj.z = 5 ; 
obj.hasOwnProperty ( 'Z'); // true 
foo.prototype.z; // still 3 
obj.z; // 5
obj.z = undefined; 
obj.z; // undefined 
delete obj.z; // true 
obj.z; // 3

3. Properties ways operation

 

4.getter and setter Introduction

5. Properties tab

6. Object Label

7. A serialized

8. Object Methods

 

Guess you like

Origin www.cnblogs.com/zdjBlog/p/11536450.html