Home > Article > Web Front-end > ES6 object assignment and Symbol
This time I will bring you the assignment and Symbol of ES6 objects. What are the precautions for using the assignment and Symbol of ES6 objects? The following is a practical case, let's take a look.
//es6语法允许变量直接为对象的赋值,快捷方便; let liu="呵呵哒"; let long="赖皮哒"; let a={liu,long}; console.log(a) ; //es6语法允许为对象构建key值; let key='skill'; var obj={ [key]:'web'} console.log(obj.skill); //es6语法允许直接合并对象; let s={liuliu:"wowoda",age:20}; let ss={long:"赖皮"}; let sss=Object.assign(s,ss); console.log(sss); //object.is()方法判断是否相等;The two equal signs will automatically perform
type conversion during comparison, while the three equal signs will not. If the type Different, it will return false directly,
and Object.is() is based on the third equal sign, and specially handles NaN, -0, +0, ensuring that -0 and +0 are no longer Same, But it should be noted that Object.is(NaN, NaN) will return true Symbol: As a newdata type was born in es6 : Symbol literally means symbolic, representing the uniqueness of something;
let myId=Symbol(); let myname=Symbol(); console.log (myname) ===>Symbol() typeof myname ===>symbol console.log(myId===myname) ===>falseYou can think of Symbol as a basic data type similar to
String; it cannot be operated with other types ;It cannot be converted implicitly;
A Symbol() can be considered as a new function created in memory (so it is not wrong to say that parentheses are the symbol of a function); The Symbol function can also pass in parameters; the parameters are only used as a description of this Symbo;let myId=Symbol("id"); let myname=Symbol("名字");Even if the parameters of two Symbols are the same, they are not equal; because a new memory space is created; The biggest use of Symbol is as an object’s
attribute to ensure uniqueness;
let system=Symbol(); let foo={}; foo[system]="windows"; //还可以保证key值为symbol类型的不被 for in遍历出来; //同样还证明了一点:对象的访问方式,要么以 . ;要么以["这里必须是字符串"];js的底层全部是字符串这种实现; console.log(foo);Symbol can also share a sign;
let sy=Symbol.for("aaa");Symbol.for( " ") does not create a new memory every time; there is only one at most; if the aaa flag does not exist in the page, create one, and if there is, directly reference the previous address; For example:
let cccc=Symbol.for("aaa");Then: console.log(sy===cccc); //true; It is also easy to understand; in the end, the uniqueness of a Symbol type with aaa description is guaranteed; I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website! Related reading:
Detailed explanation of destructuring assignment in ES6
Detailed explanation of scope and declaration of variables in ES6
The above is the detailed content of ES6 object assignment and Symbol. For more information, please follow other related articles on the PHP Chinese website!