JavaScript 中原始数据类型和非原始数据类型的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33273804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Primitive and non-primitive datatypes in JavaScript
提问by Durga Prasad
I am not able to understand exactly what is difference between primitive and non primitive data types in JavaScript even it is declared using same name i.e var.
我无法准确理解 JavaScript 中原始数据类型和非原始数据类型之间的区别,即使它使用相同的名称(即 var)声明。
采纳答案by Sudipta Kumar Maiti
Data Types (JavaScript):
Primary Data Types
The primary (primitive) data types are:
String, Number, Boolean
Composite Data Types
The composite (reference) data types are:
Object, Array
Special Data Types
The special data types are:
Null, Undefined
Click herefor details:
点击此处了解详情:
var test1 = 1;
var test2 = "Something";
var test3 = true;
var test4 = {};
var test5 = new Array();
var test6 = new Date();
var test7;
var test8 = null;
alert(typeof (test1)); //number
alert(typeof (test2)); //string
alert(typeof (test3)); //boolean
alert(typeof (test4)); //object
alert(typeof (test5)); //object
alert(typeof (test6)); //object
alert(typeof (test7)); //undefined
alert(typeof (test8)); //object
回答by Fazle Rabbi Tanjil
Javascript has five primitive data types: 1. number 2. string 3. boolean 4. undefined 5. null
Javascript 有五种原始数据类型:1. 数字 2. 字符串 3. 布尔值 4. 未定义 5. 空值
Anything that doesn't belong to any of these five primitive types is considered an object.
任何不属于这五种基本类型中的任何一种都被视为对象。
First 3 data types has a corresponding object constructor. For example
前 3 种数据类型具有相应的对象构造函数。例如
var word = "something";
And then as an object:
然后作为一个对象:
var word = new String("something");
For object constructor notice the new
keyword. It creates object reference.
对于对象构造函数,请注意new
关键字。它创建对象引用。
Another thing to notice that
还有一点要注意
var greeting = "something";
var word = new String("something");
greeting == word ----> True as their value is same
greeting === word -----> False because there value same but type is different .
As for var keyword being same for all the cases,remember that Javascript is dynamically typed language. That means it resolves data type checking during runtime rather than compile time(like Java, C++ ).
至于所有情况下 var 关键字都相同,请记住 Javascript 是动态类型语言。这意味着它在运行时而不是编译时(如 Java、C++)解决了数据类型检查。
This makes javascript extremely powerful. Though this unique feature has drawback too. Please co through this wikipedia for details: https://en.wikipedia.org/wiki/Type_system#Static_and_dynamic_type_checking_in_practice
这使得 javascript 非常强大。虽然这个独特的功能也有缺点。请通过此维基百科了解详细信息:https: //en.wikipedia.org/wiki/Type_system#Static_and_dynamic_type_checking_in_practice
Hope this helps.
希望这可以帮助。
回答by Islam Sayed
According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
根据https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
Primitive Data types in ES6 are: Boolean Null Undefined Number String Symbol
ES6 中的原始数据类型有: Boolean Null Undefined Number String Symbol
The other data type which is not a primitive one is Object
另一种不是原始数据类型是 Object
Primitive data types are immutable values.
原始数据类型是不可变值。
回答by suresh narasimman
Primitive Datatypes are 1. Number 2. String 3. Boolean 4. Undefined 5. Null
原始数据类型为 1. 数字 2. 字符串 3. 布尔值 4. 未定义 5. 空值
Non-Primitive Datatypes are 1.Object 2.Array 3.RegExp
非原始数据类型是 1.Object 2.Array 3.RegExp
回答by Ritesh kumar
Primitive datatypeare type provided by the programming language as the basic building block. like: number,string,null,undefined,Boolean.?? Primitive datatype get the memory allocation at compile time.
原始数据类型是由编程语言作为基本构建块提供的类型。如:数字、字符串、空值、未定义、布尔值。?? 原始数据类型在编译时获取内存分配。
Derived datatypeare type which are derived from the existing primitive datatype bundled together like: array,object etc. Derived datatype just get the reference to that memory allocation where value are saved.
派生数据类型是从捆绑在一起的现有原始数据类型派生的类型,如:数组、对象等。派生数据类型只是获取对保存值的内存分配的引用。
回答by Aamod Tiwari
**The latest ECMAScript standard defines eight data types:**
- String
- Boolean
- Number
- BigInt
- Null
- Undefine
- Symbol
- Object
- 细绳
- 布尔值
- 数字
- 大整数
- 空值
- 取消定义
- 象征
- 目的
Note :- Arrays do not belong to this list because they are objects as well. This is a common confusion among developers who assume that arrays are a special data type in Javascript.
注意:- 数组不属于此列表,因为它们也是对象。这是假设数组是 Javascript 中的特殊数据类型的开发人员之间的常见混淆。
Now bifurcate the data type
现在分叉数据类型
There are two types of data types in JavaScript:
JavaScript 中有两种数据类型:
Primitive values (String,Boolean,Number,BigInt,Null,Undefine,Symbol )
Non-primitive values is also called object references(Object,Array)
原始值 (String,Boolean,Number,BigInt,Null,Undefine,Symbol)
非原始值也称为对象引用(Object,Array)