Home > Web Front-end > JS Tutorial > What are the data types that are not part of javascript?

What are the data types that are not part of javascript?

藏色散人
Release: 2023-01-05 16:10:48
Original
11194 people have browsed it

The data type that does not belong to javascript is interface, which is a reserved word in js; and the data types of javascript include Undefined, Number, Symbol, etc.

What are the data types that are not part of javascript?

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

The basic data types of JavaScript are: (1) Undefined, (2) Null, (3) Boolean, (4) String, (5) Number, (6) Symbol, ( 7) Object. ES6 adds the Symbol type. The following mainly talks about some related knowledge points of the Symbol type.

Characteristics of Symbol

1. The instance is unique and unchangeable; a unique identifier The symbol can be used as the unique attribute name of the object so that others will not overwrite or overwrite the attribute value you set.

1 let id = Symbol("id");
Copy after login

2. Characteristics of data types: uniqueness, even if the values ​​generated by the same variable are not equal.

1 let id1 = Symbol("id");2 let id2 = Symbol("id");3 console.log(id1 == id2);  //false
Copy after login

3. Characteristics of data types: Hiddenness, for···in, object.keys() cannot be accessed.

1 let id = Symbol("id");2 let obj = {3     [id] : 'symbol'          
4 }5 for ( let key in obj){6     console.log(obj[key]);  //输出为空7 }
Copy after login

Accessible methods: Object.getOwnPropertySymbols, will return an array whose members are all functions of the current object Symbol value of the attribute name.

1 let id = Symbol("id");2 let obj = {3   [id] : 'symbol'      
4 }5 let arr = Object.getOwnPropertySymbols(obj);6 console.log(arr)  // [Symbol(id)]7 console.log(obj[arr[0]]) // 'symbol'
Copy after login

4. The same symbol value can be used multiple times. The official provides a global registration and registration method: Symbol.for()

let name1 = Symbol.for("name"); // 检测未创建后新建let name2 = Symbol.for("name"); // 检测已创建后返回console.log(name1 == name2); // true
Copy after login

In this way, you can get the global symbol object through the parameter value. On the contrary, you can use Symbol.keyFor()Get the parameter value.

let name1 = Symbol.for("name");
let name2 = Symbol.for("name");
console.log(Symbol.keyFor("name1")); // 'name'console.log(Symbol.keyFor("name2")); // 'name'
Copy after login

5. The result of instanceof is false

let s = Symbol('foo');
console.log(s instanceof Symbol); // false
Copy after login

Note: The parameters when creating symbol type data are just Used as an identifier, so Symbol() can also be used as a parameter.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What are the data types that are not part of javascript?. 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