This is the third day of my participation in the August Text Challenge.More challenges in August
The assignment
- Primitive type variable assignment
- Reference type variable assignment
preface
Before ES6, JS had six data types, which were composed of five basic types and one reference type. The five basic types were undefined, NULL, Boolean, Number, string, and one reference type was Object
However, with the popularity of ES6 (from new knowledge to basic knowledge), a new member named Symbol is added, and the data type pattern of JS changes to six basic types + one reference type. Symbol is listed in the basic type queue, while the reference type remains unchanged
Primitive type variable (Primitive Value)
- undefined
- null
- boolean
- number
- string
- symbol
object
In addition, the above six basic types of actual output in the chrome71 console
undefined
typeof undefined
"undefined"
Copy the code
Undefined does not capitalize the first letter
null
typeof null
"object"
Copy the code
The type of an empty object is of course an object
boolean
typeof Boolean(true)
"boolean"
Copy the code
Boolean does not capitalize the first letter
Boolean is a global JS method provided by the window object that converts a value to a Boolean type.
Boolean(a)false
Boolean(false)
false
Boolean(0)
false
Boolean(111)
true
Boolean(' ')
false
Boolean(undefined)
false
Boolean(null)
false
Boolean('false')
true
Copy the code
number
typeof Number(a)"number"
Copy the code
Like Boolean(), Number() is the built-in method for converting a variable to type Number, as shown in the following example:
Number(a)0
Number(1)
1
Number(false)
0
Number(true)
1
Number(' ')
0
Number('false')
NaN
Number('true')
NaN
Number('1')
1
Number(undefined)
NaN
Number(null)
0
Number('1str')
NaN
Number('str1')
NaN
Copy the code
As you can see, the Number() method does its best to convert a numeric string to Number, but string variables that contain strings or are simply strings do not. The Number() method returns a special NaN. It is a built-in js constant of type number, which is used to represent a non-number.
typeof NaN
"number"
Copy the code
string
typeof String(a)"string"
Copy the code
Boolean(); Number(); string (); string (); string (); string ();
String(a)""
String('boolean')
"boolean"
String('number')
"number"
String(Number('false'))
"NaN"
String(1)
"1"
String(undefined)
"undefined"
String(null)
"null"
typeof String(undefined)
"string"
typeof String(null)
"string"
Copy the code
NaN, undefined, and null are all String types, and they are all typed as String()
symbol
typeof Symbol(1)
"symbol"
typeof Symbol(a)"symbol"
Copy the code
The above are the six basic types of traditional mainstream JS (JS under es5 standard implementation)
Reference type variable (reference value)
First, all reference types belong to the object type, including array, function, object, date, regexp, and so on
Note: Reference types can no longer be determined by typeof, because reference types are derived from object. Typeof references are used to determine the distinction between basic types
typeof Array(a)"object"
typeof new Date(a)"object"
typeof function a(){}
"function"
typeof []
"object"
Copy the code
object
typeof Object(a)"object"
Copy the code
The object () method is used to convert everything into js objects, but this is not the same as String(), Boolean(), Number(). Object is the root and source of JS. Without it there would be nothing else with JS
typeof Object(a)"object"
Object()
{}
typeof new Date(a)"object"
typeof Object(a)"object"
Object(1)
Number {1}
typeof Number(1)
"number"
Object(null)
{}
typeof Object(null)
"object"
typeof Object(false)
"object"
Object(false)
Boolean {false}
Copy the code
features
The value of a reference type is mutable, as opposed to immutable, so copying data of a primitive type is really copying a new value of the same type. But a reference type variable is a pointer to a piece of memory space, and so on the direct copy a reference type just to get a pointer to a pointer to another with a piece of memory space, but said the value is the same before and after the copy, so if change the value of the original reference variables, at this time will change the copy of the value of a variable at the same time, for example:
var x = {name: 'hello'}
undefined
var y = x;
undefined
x
{name: "hello"}
y
{name: "hello"}
x.name = 'world'
"world"
x
{name: "world"}
y
{name: "world"}
Copy the code
supplement
Instanceof determines whether an object instance is an instanceof an object. The ptotoType attribute is used to determine whether the prototype property of a constructor refers to an object on the ptototype chain of another object to be tested.
({}) instanceof Object
true
([]) instanceof Array
true
(/a/) instanceof RegExp
true
(function() {}) instanceof Function
true
Copy the code
This article is the integration of various high-quality content and personal understanding of the arrangement, refer to the article as follows:
- JavaScript has an in-depth understanding of values for base and reference types