[JS011] the original data type of ES6 learning notes is Symbol

Posted by zenix on Sun, 19 Dec 2021 05:14:55 +0100

Date: August 23, 2021
Author: Commas
Note: if you think it's helpful, please give me a favor. You can also pay attention to me and we can grow together; If there is something wrong, I hope you guys will give me advice. Thank you ^ -^
(งงง) learning notes of ES6
1.01365 = 37.7834;0.99365 = 0.0255
1.02365 = 1377.4083;0.98365 = 0.0006

1, Introduction and creation of Symbol

Note: Symbol is a new original data type introduced by ES6. It is mainly used to represent unique values. It is the seventh data type of JavaScript language, which is a bit similar to the data type of string.
PS: Symbol function stack cannot use the new command because Symbol is the original data type, not an object.

  • Syntax 1: let symbolValue = Symbol(variable)
//(1) Create a Symbol without [ID]
let s1 = Symbol();
console.log(s1);
//Console output: Symbol()

//(2) Create a Symbol with [ID], and the ID is "Zhang San"
let s2 = Symbol("Zhang San");
console.log(s2);
//Console output: symbol (Zhang San)
  • Syntax 2: let symbolvalue = symbol for(variable)
//(1) Create a Symbol without [ID]
let s1 = Symbol.for();
console.log(s1);
//Console output: Symbol(undefined)

let s3 = Symbol.for("");
console.log(s3);
//Console output: Symbol(undefined)

//(2) Create a Symbol with [ID], and the ID is "Zhang San"
let s2 = Symbol.for("Zhang San");
console.log(s2);
//Console output: symbol (Zhang San)

2, Characteristics of Symbol

  1. The value of Symbol is unique and can be used to solve the problem of naming conflict;
//(1) Create a Symbol without [ID]
//(1-1) create Symbol
let s1 = Symbol();
console.log(s1);
//Console output: Symbol()

//(1-2) same Symbol
let s1_same = s1;
console.log(s1_same,s1===s1_same);
//Console output: Symbol() true

//(1-3) different symbols
let s1_other = Symbol();
console.log(s1_other,s1===s1_other);
//Console output: Symbol() false

//(2) Create a Symbol with [ID], and the ID is "Zhang San"
//(2-1) create Symbol
let s2 = Symbol("Zhang San");
console.log(s2);
//Console output: symbol (Zhang San)

//(2-2) same Symbol
let s2_same = s2;
console.log(s2_same,s2===s2_same);
//Console output: symbol (Zhang San) true

//(2-3) different symbols
let s2_other = Symbol("Zhang San");
console.log(s2_other,s2===s2_other);
//Console output: symbol (Zhang San) false

Summary: Symbol("Zhang San")== Symbol("Zhang San") is not discussed in depth. Just from the performance point of view, although the equal sign is "Zhang San" before and after, the value of "Zhang San" of symbol is different, so it can be used to solve the problem of naming conflict;

  1. The value of Symbol cannot be calculated with other data;
//(1) Create a Symbol without [ID]
let s1 = Symbol();
// console.log(s1+'199');
//Console output:
//Uncaught TypeError: Cannot convert a Symbol value to a string

//(2) Create a Symbol with [ID], and the ID is "Zhang San"
let s2 = Symbol("Zhang San");
console.log(s2+'007');
//Console output:
//Uncaught TypeError: Cannot convert a Symbol value to a string
  1. Symbol defined object properties cannot use for in... Loop traversal, but you can use Reflect.ownkeys To get all the key names of the object;
const obj1 = {
    [Symbol("p1")]: "Zhang San",
    p1: "p1",
    p2: 18,
    [Symbol("p2")]: function(s){
        console.log(s);
    },
    [Symbol("p2")]: function(s2){
        console.log(s2);
    }
};
console.log(Reflect.ownKeys(object1));
//Console output:
//(5) ["p1", "p2", Symbol(p1), Symbol(p2), Symbol(p2)]

You can use another object to store the Symbol value, so it is convenient to call the properties or methods of this object, as shown below:

const oM={
    "name":Symbol("p1"),
    "s":Symbol("p2"),
    "s2":Symbol("p2"),
}

const obj1 = {
    [oM.name]: "Zhang San",
    p1: "p1",
    p2: 18,
    [oM.s]: function(s){
        console.log(s);
    },
    [oM.s2]: function(s2){
        console.log(s2);
    }
};
console.log(Reflect.ownKeys(obj1));
//Console output: (4) [P1, P2, symbol (P1), symbol (P2)]
console.log(obj1[oM.name],obj1.p2);
//Console output: Zhang San 18

3, On seven types of original data

Serial numberdata typetypeof valuedescribe
1Number"number"numerical value
2Stringstringcharacter string
3Boolean"boolean"Boolean type, true or false
4Object"object"Object (typeof: object, array or null return object)
5null"object"null is "nothing" and is regarded as something that does not exist
6undefined"undefined"Undefined, null and undefined are not the same thing
7Symbol"symbol"Independent "string"
let a = 1
    ,b = "123"
    ,c = true
    ,d = new Object()
    ,e = []
    ,f = null
    ,g = undefined
    ,h = Symbol()
    ,i = function(a){}

    console.log(a,typeof(a));//1 "number"
    console.log(b,typeof(b));//123 string
    console.log(c,typeof(c));//true "boolean"
    console.log(d,typeof(d));//{} "object"
    console.log(e,typeof(e));//[] "object"
    console.log(f,typeof(f));//null "object"
    console.log(g,typeof(g));//undefined "undefined"
    console.log(h,typeof(h));//Symbol() "symbol"
    console.log(i,typeof(i));//ƒ (a){} "function"

Copyright notice: This article is the original article of the blogger. If you need to reprint it, please give:
Original link: https://blog.csdn.net/qq_35844043/article/details/119866942

Topics: Javascript