Home > Web Front-end > JS Tutorial > body text

ES6 object assignment and Symbol

php中世界最好的语言
Release: 2018-03-10 15:00:26
Original
1767 people have browsed it

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.

Object assignment merge:

//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()方法判断是否相等;
Copy after login

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 new

data 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)        ===>false
Copy after login

You 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("名字");
Copy after login

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);
Copy after login

Symbol can also share a sign;

 let sy=Symbol.for("aaa");
Copy after login

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");
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!