JavaScript is a weak type , or dynamic language. This means that you do not have to declare the type of the variable in advance, and the type is automatically determined during the program's run. This also means that you can use the same variable to hold different types of data
According to ECMAScript 5.1, JavaScript has six data types, respectively:,,,, Undefined
, Null
Boolean
Number
String
Object
. The first five types belong to the base type, and the last one belongs to the object type.
The latest ECMAScript 6 adds another type: Symbol (ECMAScript 6 new definition)
Basic data types
Undefined:
There is only one value, which undefined
means "null (no Value)", which applies to all data types.
Null:
Only one value, for null
, means "null object", which applies only to object types. (literal)
Boolean
: There are two values for the true
false
Number
: The value is a collection of 64-bit floating-point numbers that follow the IEEE 754 standard, with no integer data structures. Also contains three special values: NaN
,, Infinity
-Infinity
String
: The value is a collection of Unicode characters that are poor. Must be used ‘
or "
enclosed.
A, String
The string type of JavaScript is used to represent text data. It is an "element" of a set of 16-bit unsigned integer values. Each element in the string occupies the position of the string. The index of the first element is 0, the next is index 1, and so on. The length of the string is the number of its elements
Unlike the C language, the string in JavaScript is immutable (for example, the operation of a string in JavaScript must return a new string, the original string has not been changed)
Everything in JavaScript is object-based.
There are two types of string creation
1. String created using literal method, as a basic type of string//string
2. Using String()
the created string, as the base type of string//String
3. A string created using a constructor new String()
,//string for the object type
var str1 = "JavaScript"; Stringvar str2 = String ("javascript"); The string does not recommend var str3 = new String (' JavaScript '); Object
There is a difference between an object and a string.
S1 = "2 + 2"; Creates a string primitives2 = new String ("2 + 2"); Creates a String Objectconsole.log (eval (S1)); Returns the number 4console.log (eval (S2)); Returns the string "2 + 2"
Conversion of a String object valueof-"string
Console.log (eval (s2.valueof ())); Returns the number 4
Second, Boolean
Do not confuse the original value with true false, and the Boolean object with a value of true false
1, if the parameter of the Boolean constructor is not a Boolean value, the argument is converted to a Boolean value
2, if the parameter is,,,, 0
-0
null
false
NaN
undefined
, or an empty string (""), the value of the generated Boolean object is False. Any other value, including any object or string "false"
, will create a Boolean object with a value of true
var x = new Boolean (false); if (x) {Console.log (x.valueof (), typeof x);//False object}
It's going to execute, it's amazing code.
Do not convert Boolean
a non-Boolean value to a Boolean value by means of a new object. Using the Boolean function directly is correct
var x = Boolean (expression); This is done with var x = new Boolean (expression); And don't do that!
The time of initialization
Falsevar Bnoparam = new Boolean (), var bzero = new Boolean (0), var bnull = new Boolean (null), var bemptystring = new Boolea N (""), var bfalse = new Boolean (false),//truevar btrue = new Boolean (true), var btruestring = new Boolean ("true"); var bfalse String = new Boolean ("false"), var Bsulin = new Boolean ("Su Lin");
Third, number
According to the ECMAScript standard, there is only one numeric type in JavaScript: The value of a double-precision 64-bit binary format based on the IEEE 754 standard (-(253-1) to 253-1). It does not give a specific type for integers. In addition to being able to represent floating-point numbers, there are some signed values: +Infinity
, -Infinity
and NaN
(non-numeric, not-a-number)
A numeric type has only one integer, and it has two representations: 0 can be expressed as-0 and +0 ("0" is a shorthand for +0). In practice, this has little effect. For example, +0 === -0
true. However, you may want to pay attention to dividing by 0:
42/+0; Infinity42/-0; -infinity
If the argument cannot be converted to a number, it is returned NaN
.
In a non-constructor context (for example: there is no new operator), it Number
can be used to perform type conversions
IsNAN type judgment
Number.isnan (NaN); Truenumber.isnan (Number.NaN); Truenumber.isnan (0/0) //true//e.g. these would has been true with Global IsNaN () Number.isnan ("NaN"); Falsenumber.isnan (undefined); Falsenumber.isnan ({}); Falsenumber.isnan ("BlaBla"); false//these all return Falsenumber.isnan (true); Number.isnan (NULL); Number.isnan (37); Number.isnan ("37"); Number.isnan ("37.37"); Number.isnan (""); Number.isnan ("");
The relationship of the prototype chain inheritance
Console.log (number.prototype.__proto__ = = Object.prototype); Trueconsole.log (number.prototype.__proto__.__proto__ = = object.prototype.__proto__);//trueconsole.log ( object.prototype.__proto__ = = = null);//trueconsole.log (typeof number);//function
Using a Number
Transform Date
object
var d = new Date ("December, 1995 03:24:00"); Console.log (number (d));
Four, Null
null
is a JavaScript literal that represents a null value (null or an "empty" value), i.e. no object is rendered (no objects value is present). It is one of the original JavaScript values.
null
is a literal (not a property of a global object, undefined
Yes)
Console.log (NULL); Nullconsole.log (undefined);//undefinedconsole.log (window.null);//undefinedconsole.log (window.undefined);// Undefined
The difference between null and undefined
Console.log (foot);//uncaught Referenceerror:foot is not Definedvar foo;console.log (foo);//undefinedvar bar =null; Console.log (bar);//nulltypeof null //Object (bug in ECMAScript, should is null) typeof undefined // Undefinednull = = = undefined//falsenull = = undefined//True
So judging null, you can judge the type + value
Wu, Undefined
在JavaScript中,undefined这个词有多重含义.首字母大写的Undefined表示的是一种数据类型,小写的undefined表示的是属于这种数据类型的唯一的一个值.但这两种undefined都只能存在于文档或规范中,不能存在于JavaScript代码中.在JavaScript代码中,你看到的undefined最有可能是全局对象的一个属性
, the initial value of the property is the original value mentioned earlier, and there is the undefined
case that this undefined
is a local variable, like other ordinary variables, there is no particularity, its value is not necessarily undefined
, but usually is the case. What we're saying undefined
, 都指的是window.undefined
This property.
In ES3 (before Firefox4), window.undefined
is a normal property, you can completely change its value into arbitrary truth, but in ES5 (after Firefox4), window.undefined
a non-writable, non-configurable data property, its value is always undefined
.
The value of an uninitialized variable is the undefined
value of a parameter variable that does not have an incoming argument, which is undefined
returned by default if a function does not return anything undefined
.
You can use the strict equality operator to determine whether a value is undefined
:
var foo;console.log (foo = = = undefined);//trueconsole.log (typeof foo = = = ' undefined ');//trueconsole.log (Window.foo = = = undefined);//trueconsole.log (bar = = = undefined);//uncaught Referenceerror:bar is not definedconsole.log (typeof bar = = = ' undefined ');//trueconsole.log (Window.bar = = = undefined);//trueconsole.log (typeof undefined = = ' undefined '); Trueconsole.log (typeof null = = ' object ');//trueconsole.log (null = = undefined);//trueconsole.log (null = = = undefined) ;//false
Summarize
Null
null
represents an empty object pointer, without pointing to any object
Undefined
Indicates that the undefined
property of the declaration variable or object is not initialized
undefined
Values are derived from null
, so performing an equality test on them will returntrue
- There are methods for numeric, Boolean, object, and string values
toString()
. But null
and undefined
the value does not have this method
In most cases, the calling toString()
method does not have to pass parameters. However, when you call a method of a numeric value toString()
, you can pass a parameter: the cardinality of the output value
var num = 10;alert (num.tostring ()); "Ten" Alert (num.tostring (2)); "1010" Alert (num.tostring (8)); alert (num.tostring); "Ten" alert (num.tostring); A
null
undefined
You can also use a transform function String()
, which converts any type of value to a string, without knowing whether the value to be converted is or not. The String()
function follows the following translation rules:
If the value has a toString()
method, the method is called (no arguments) and the corresponding result is returned
If the value is null
, then return " null
"
If the value is undefined
, then return " undefined
"
VI, Object
Everything in JavaScript is Object
objectstypeof {A:1} = = = ' object ';//Use the Array.isarray or Object.prototype.toString.call method to differentiate the array type from the base Object typeof [1, 2, 4] = = = ' object '; typeof new Date () = = = ' object ';//The following is easily confusing, do not use it! typeof New Boolean (true) = = = ' object '; typeof new Number (1) = = = ' object '; typeof new String ("abc") = = = ' object ';//function typeof function () {} = = = ' function '; typeof Math.sin = = = ' function ';
Instantiate an empty object
var o = new Object (), var o = new Object (undefined), var o = new Object (null), var o = {};
Prototype
When you define a property of __proto__: 值
or "__proto__": 值
, the name is not created__proto__属性。如果给出的值是对象或者
null
,那么对象的[[Prototype]]会被设置为给出的值。(如果给出的值不是对象也不是null,那么对象的原型不会改变。)
var obj1 = {};assert (object.getprototypeof (obj1) = = = Object.prototype), var obj2 = {__proto__: null};assert (OBJECT.GETPR Ototypeof (obj2) = = = null), var protoobj = {};var obj3 = {"__proto__": Protoobj};assert (object.getprototypeof (obj3) = = = PR Otoobj) var obj4 = {__proto__: "not a object or null"};assert (object.getprototypeof (obj4) = = = Object.prototype); assert ( !obj4.hasownproperty ("__proto__"));
In object literals, there is only one chance to change the prototype, and multiple changes to the prototype are considered syntax errors.
A property definition that does not use the colon notation does not alter the object's prototype, but rather is a normal attribute definition as with other attributes that have different names.
var __proto__ = "variable"; var obj1 = {__proto__};assert (object.getprototypeof (obj1) = = = Object.prototype); assert (obj1 . hasOwnProperty ("__proto__")), assert (obj1.__proto__ = = = "Variable"), var obj2 = {__proto__ () {return ' Hello ';}}; ASSERT (obj2.__proto__ () = = = "Hello"), var obj3 = {["__prot" + "o__"]:};assert (obj3.__proto__ = = = 17);
The difference from JSON
- JSON only allows
"property": value
attribute definitions in the form of syntax. Property names must be enclosed in double quotation marks. And the attribute definition does not allow the use of simple notation.
- In JSON, the value of a property allows only strings, numbers, arrays, true,false, or other JSON objects.
- JSON, the value is not allowed to be set to a function.
Date
the object, JSON.parse()
after processing, becomes a string.
JSON.parse()
The computed property name is not processed and is thrown as an error.
DefineProperty
Object.defineProperty()
method to define a new property directly on an object, or to modify an existing property and return the object
Using __proto__object.defineproperty (obj, "key", { __proto__: null,//No inherited property value: "Static" //No Enumerable //No configurable //No writable// as Default});//explicit Object.defineproperty (obj, "key", { Enumerable:false, Configurable:false, writable:false, value: "Static"});//Reclaim the same object function Withvalue ( Value) { var d = WITHVALUE.D | | ( WITHVALUE.D = { Enumerable:false, writable:false, configurable:false, value:null } ); D.value = value; return D;} // ... And... Object.defineproperty (obj, "key", Withvalue ("Static"));//If Freeze is available, prevent code additions//value, get, set, enumerable, writable, CO nfigurable//to the Object prototype (object.freeze| | Object) (Object.prototype);
configurable
When and only if the property descriptor value is true, the property may change or be deleted from the corresponding object. The default false
is .
enumerable
true
When and only if the property appears in the corresponding object enumeration property. default is false。
value
The value associated with the property. can be any valid JavaScript value (numeric, object, function, etc.). The default undefined
is .
-
writable
true
When and only if the value associated with the property can be changed with the assignment operator.
The default false
is .
The access descriptor also has the following optional key values:
-
get
A method that provides a getter to a property, or if no getter is available
undefined
. method returns the value that is used as the property.
The default undefined
is .
-
set
A method that provides a setter to a property, if there is no setter
undefined
. The method assigns a new value as a unique parameter to the property.
The default undefined
is .
Reference sources
Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects
What is the data type in JavaScript?