Home >Backend Development >PHP Tutorial >What are the differences in how these two types of object literals (json) are operated in JavaScript?

What are the differences in how these two types of object literals (json) are operated in JavaScript?

WBOY
WBOYOriginal
2016-07-06 13:53:391050browse

1. Does object literal === json type object?
2. Suppose there is a json type object, such as var a={};
3. What is the difference between operating json in the following ways:
①a.propertyA=valueA;
②var a={propertyA :valueA};

It is a way of defining attributes internally and externally

Reply content:

1. Does object literal === json type object?
2. Suppose there is a json type object, such as var a={};
3. What is the difference between operating json in the following ways:
①a.propertyA=valueA;
②var a={propertyA :valueA};

It is a way of defining attributes internally and externally

1. The object is a reference type, the === method is not true;

2. There is no difference between a.propertA=valueA and a={propertyA:valueA};. But please note that there is a difference if it is var a=function(){};a.prototype.propertyA=valueA and a.prototype={propertyA:valueA}.

  1. There is no such thing as a JSON type object. var obj = {}; is called an object literal. JSON is the same syntax format as XML.

  2. a={propertyA:valueA}; is to directly assign a value to a literal. a.propertyA=valueA or a[propertyA]=valueA can be used to access/modify/increase the attribute value of a literal. (When using the dot operator, propertyA must be a legal JavaScript identifier) ​​

1. Object literal refers to using key/value to define objects. It is simple and clear. There is no limit on the value. The value of json can only be: numbers (integers or floating point numbers), strings (in (in double quotes), logical value (true or false), array (in square brackets), object (in curly brackets), null. In other words, the value of json cannot be a function. So there is a difference between the two.
2, a.propertyA = valueA; is equivalent to adding an attribute to the object, and the previously defined attributes still exist. And a = {propertyA: valueA}; overwrites a, that is, the attributes in the previous a have been overwritten. The a object only has the attribute propertyA.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn