First, the grammar
Two forms of definition: text form and construction form.
// text Form var myObj = { Key:value}; // Construction Form var New = value;
Second, type
Objects are the basis of JavaScript.
1) Basic type
There are six main types in JavaScript (the term is "language type"):
String, number,boolean,null, Undefined, object
2) built-in objects
There are also some object subtypes in JavaScript, often referred to as built-in objects.
String, number, Boolean, Object, Function, Array, Date, REGEXP, Error
The engine can automatically convert some of the underlying types into the corresponding built-in objects, and then it can invoke the object's properties or methods.
// Convert a string to a string object var strprimitive = "I am a String"// // "M"// Convert numbers to Number object var num = 42.359.toFixed (2); Console.log (num) ; // 42.36
Third, the content
1) Properties
var myObject =2// 2 Property access // 2 Key Access
. A syntax is often referred to as " property access ", and ["a"] syntax is often referred to as " key access ".
2) Copying objects
There is an ingenious method of copying JSON security (that is, it can be serialized as a JSON string and can parse an object with exactly the same structure and value based on the string):
var newObj = Json.parse (json.stringify (someobj));
Shallow copy is easy to understand and has much less problem than deep copy, so ES6 defines object.assign (..) method to achieve shallow replication.
3) Property Descriptor
Starting with ES5, all attributes have a property descriptor. Can be implemented by means of object.defineproperty () .
var myObject ="A", { 2, true, true , true // 2
Vue.js is the way to achieve tracking changes.
4) Getter and setter
In ES5, the getter and setter sections can be used to override the default action, but only on a single property and cannot be applied across the entire object.
varMyObject = { //define a getter for aget A () {return2; }};object.defineproperty (MyObject,//target Object"B",//Property name{//Descriptor //set a getter for BGetfunction() { return This. A * 2 }, //Make sure B appears in the object's property listEnumerabletrue}); Console.log (MYOBJECT.A); //2Console.log (myobject.b);//4
5) The existence of
The in operator checks whether the property is in the object and its [[Prototype]] prototype chain.
hasOwnProperty (..) Only checks if the property is in the MyObject object and does not check the [[Prototype]] chain.
There is a previous "JavaScript in typeof, ToString, Instanceof, constructor and in" made some comparisons.
var myObject = { 2};(in// truein// myobject.hasownproperty (// true// false
Four, traverse
Unlike arrays, normal objects do not have a built-in @ @iterator, so they cannot be automatically completed for. of traversal.
But you can define @ @iterator for any object you want to traverse.
varMyObject ={A:2, B:3};object.defineproperty (MyObject, Symbol.iterator, {enumerable:false, writable:false, Configurable:true, Value:function() { varn \ This; varIDX = 0; varKS =Object.keys (o); return{Next:function() { return{value:o[ks[idx++]], done: (IDX>ks.length)}; } }; }});//Manually traverse MyObjectvarit =Myobject[symbol.iterator] (); It.next ();//{value:2, done:false}It.next ();//{value:3, done:false}It.next ();//{value:undefined, done:true}//use for. of Traverse MyObject for(varV of MyObject) {Console.log (v);}//2//3
JavaScript you don't Know (c)--object