Home Web Front-end JS Tutorial ES6 object assignment and Symbol

ES6 object assignment and Symbol

Mar 10, 2018 pm 03:00 PM
object Assignment

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to enable copy and paste for VMware virtual machines How to enable copy and paste for VMware virtual machines Feb 21, 2024 am 10:09 AM

You can easily copy and paste text and files between VMware virtual machines (VMs) and physical systems. This capability allows you to easily transfer images, formatted and unformatted text, and even email attachments between virtual machines and host systems. This article will show you how to enable this feature and demonstrate methods for copying data, files, and folders. How to Enable Copy/Paste in VMware VMware provides three different ways to copy data, files or folders from a virtual machine to a physical computer and vice versa, as explained below: Copy and Paste Elements Drag and Drop Feature Folder Sharing 1 ] Enable copy-paste using VMware Tools You can use the keyboard if your VMWare installation and guest operating system meet the requirements

How to copy a page in Word How to copy a page in Word Feb 20, 2024 am 10:09 AM

Want to copy a page in Microsoft Word and keep the formatting intact? This is a smart idea because duplicating pages in Word can be a useful time-saving technique when you want to create multiple copies of a specific document layout or format. This guide will walk you through the step-by-step process of copying pages in Word, whether you are creating a template or copying a specific page in a document. These simple instructions are designed to help you easily recreate your page without having to start from scratch. Why copy pages in Microsoft Word? There are several reasons why copying pages in Word is very beneficial: When you have a document with a specific layout or format that you want to copy. Unlike recreating the entire page from scratch

Convert an array or object to a JSON string using PHP's json_encode() function Convert an array or object to a JSON string using PHP's json_encode() function Nov 03, 2023 pm 03:30 PM

JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

Use Python's __contains__() function to define the containment operation of an object Use Python's __contains__() function to define the containment operation of an object Aug 22, 2023 pm 04:23 PM

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Source code exploration: How are objects called in Python? Source code exploration: How are objects called in Python? May 11, 2023 am 11:46 AM

Wedge We know that objects are created in two main ways, one is through Python/CAPI, and the other is by calling a type object. For instance objects of built-in types, both methods are supported. For example, lists can be created through [] or list(). The former is Python/CAPI and the latter is a calling type object. But for instance objects of custom classes, we can only create them by calling type objects. If an object can be called, then the object is callable, otherwise it is not callable. Determining whether an object is callable depends on whether a method is defined in its corresponding type object. like

Unlock macOS clipboard history, efficient copy and paste techniques Unlock macOS clipboard history, efficient copy and paste techniques Feb 19, 2024 pm 01:18 PM

On Mac, it's common to need to copy and paste content between different documents. The macOS clipboard only retains the last copied item, which limits our work efficiency. Fortunately, there are some third-party applications that can help us view and manage our clipboard history easily. How to View Clipboard Contents in Finder There is a built-in clipboard viewer in Finder, allowing you to view the contents of the current clipboard at any time to avoid pasting errors. The operation is very simple: open the Finder, click the Edit menu, and then select Show Clipboard. Although the function of viewing the contents of the clipboard in the Finder is small, there are a few points to note: the clipboard viewer in the Finder can only display the contents and cannot edit them. If you copied

Disable or enable automatic copy selection for copying in Terminal Disable or enable automatic copy selection for copying in Terminal Mar 24, 2024 am 09:46 AM

This article will show you how to enable or disable automatic copying of selections to the clipboard in Windows Terminal. Windows Terminal is a multi-tab terminal emulator developed by Microsoft specifically for Windows 11/10, replacing the traditional command prompt. It supports running applications such as Command Prompt, PowerShell, WSL, Azure, etc. Often when working in the terminal, users need to copy commands and output, however the terminal does not support copying selection operations by default. Keep reading this article to learn how to fix this issue. How to enable or disable automatic copying of selections to cache in Terminal? Here's how you can enable or disable automatic copying of selections to the Terminal clipboard: Open the Terminal application and click above

See all articles