Objects and object label of JavaScript object serialization

There are three objects Tags: proto, class and extensible.

A, proto label

For example, I created a new person object, which points to __proto__ Person.prototype, then Person.prototype of __proto__ points to Object.prototype, Object.prototype of __proto__ points to null, so as to form a complete prototype chain .

Two, class label

Points on the graph are:

1. Object.prototype.toString.call (o) is equivalent to o.toString (), o as this passed, much like with usage apply.

2.slice (8, -1) that is cut out of the string of eight characters to a last character (the last character is not included), designed to cut out [object Xxx] in Xxx.

3.typeof new Number (1) return object, but the toString returns a Number, toString more accurate number. (Typeof null returns the object, and toString null is returned.)

Three, extensible tag

 

Points on the graph are:

1. isExtensible determines whether an extended attribute, with preventExtensions to prevent expansion, object properties The properties of the original tag is not affected.

2. isSealed determines whether the seal, seal used to seal an object, the original object attribute labels at this time becomes unavailable arranged.

3. isFrozen determination is frozen, freeze used to freeze the object, the original object attribute labels at this time becomes a non-writable and not configurable.

4. These three operations impact on the properties of the label deepened step by step, but does not affect the operation of the case where the object prototype chain (prototype chain) of the front end portion.

 

Next we learn at the target sequence.

 

Points on the graph are:

1. The object becomes the string is called the target sequence, the target sequence of the key will be in double quotation marks, before and after the braces are subject double quotes.

2.若对象属性为undefined,则序列化后属性便去掉了。若属性值为NaN或Infinity(无穷),则序列化后属性会变为null。若属性值为Date对象,则序列化后会变为UTC格式。

 

上图的要点为:对象内部加上toJSON方法,从而自定义序列化过程。

 

上图的要点为:

1.obj.toString()可直接输出[object Object]字符串,相当于Object.prototype.toString.apply/call(obj)。

2.可在对象内部重写toString和valueOf方法。

3.+obj首先会调用valueOf,看能否变成非对象,若不能,则调用toString,若仍不能转成非对象,则报错。

Guess you like

Origin www.cnblogs.com/luoyihao/p/12231424.html