Although JavaScript is a weakly typed language, it also has several data types of its own, namely number, String, Boolean, Object, udefined, and Null. Where object belongs to a complex data type, object is made up of unordered key-value pairs. The remaining few are simple data types. Note: The first letter of the variable type is uppercase, and the variable value is lowercase.
JavaScript does not support custom types, so all values in JavaScript belong to one of these six types.
According to the ECMAScript 5.1 specification, JavaScript has six types of data, namely Undefined, Null, Boolean,number, String, Object. The first five are of the basic 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: Only one value, for Undefined, means "null value (no value)", which applies to all data types.
Null: Only one value, NULL, means "empty object (No object)" and applies only to object types. (literal)
Boolean: Two values, true and False
Number: The value is a collection of 64-bit floating-point numbers that follow the IEEE 754 standard, and there is no integral data structure. In addition, three special values are included: NaN, Infinity,-infinity
String: A value is a collection of poor Unicode characters. must be enclosed in ' or '.
One, 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 first element has an index of 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 strings in JavaScript are immutable (for example, the operation of a string in JavaScript must return a new string, and the original string is not changed)
Everything in JavaScript is object-based.
There are two types of string creation
1. A string//string of the base type, created using a literal method
2. Strings created using string (), string//string of base type
3, the string created using the constructor new string (),//string for the object type
var str1 = "JavaScript"; String
var str2 = String ("javascript"); string is not recommended
var str3 = new String (' JavaScript '); Object
Object and string are also different.
S1 = "2 + 2"; Creates a string primitive
S2 = new String ("2 + 2"); Creates a String object
Console.log (eval (S1)); Returns the number 4
Console.log (eval (S2)); Returns the string "2 + 2"
Conversion of String Objects valueof-"string"
Console.log (eval (s2.valueof ())); Returns the number 4
Two, Boolean
Do not confuse the original value true False with a Boolean object with a value of true false
1. If the Boolean constructor argument 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", creates 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, amazing code.
Do not convert a non-Boolean value to a Boolean value by creating a new Boolean object. It is correct to use Boolean functions directly
var x = Boolean (expression); So with
var x = new Boolean (expression);//And not!
When initialized
//false
var bnoparam = new Boolean ();
var bzero = new Boolean (0);
var bnull = new Boolean (null);
var bemptystring = new Boolean ("");
var bfalse = new Boolean (false);
True
var btrue = new Boolean (true);
var btruestring = new Boolean ("true");
var bfalsestring = 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 of integer. 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 represented as 0 and +0 ("0" is shorthand for +0). In practice, this also has little effect. For example +0 = = 0 is true. However, you may want to pay attention to dividing by 0:
42/+0; Infinity
42/-0; -infinity
Returns NaN if the parameter cannot be converted to a number.
Number can be used to perform type conversions in a non-constructor context (for example, no new operator)
isNaN type judgment
Number.isnan (NaN); True
Number.isnan (Number.NaN);//True
Number.isnan (0/0) /true
//e.g. these would have been true W ITH global isNaN ()
Number.isnan ("NaN"); False
Number.isnan (undefined);//False
Number.isnan ({}) ; False
Number.isnan ("BlaBla");//False/all of return
false
Number.isnan (true)
; Number.isnan (null);
Number.isnan (Panax Notoginseng);
Number.isnan ("Panax Notoginseng");
Number.isnan ("37.37");
Number.isnan ("");
Number.isnan ("");
Relationship of prototype chain inheritance
Console.log (number.prototype.__proto__ = = Object.prototype); True
Console.log (number.prototype.__proto__.__proto__ = = object.prototype.__proto__);//true
Console.log (object.prototype.__proto__ = = null);//true
Console.log (typeof number);//function
Convert Date object with number
var d = new Date ("December 17, 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), that is, no objects are rendered (no object value is present). It is one of the original JavaScript values.
Null is a literal (not a property of a global object, undefined)
Console.log (NULL); Null
Console.log (undefined);//undefined
Console.log (window.null);//undefined
Console.log (window.undefined);//undefined
The difference between null and undefined
Console.log (foot);//uncaught referenceerror:foot is not defined
var foo;
Console.log (foo);//undefined
var bar =null;
Console.log (bar);//null
typeof null //Object (bug in ECMAScript, should is null)
typeof undefined//Undefin ed
NULL = = undefined//false
null = undefined//True
So to judge Null, you can determine the type + value
Five, Undefined
In JavaScript, the word undefined has multiple meanings. The first-letter uppercase undefined represents a data type, A lowercase undefined represents a unique value that belongs to this data type. But both of these undefined can only exist in documents or specifications and cannot exist in JavaScript code. In JavaScript code, The undefined you see is most likely a property of a global object, the initial value of which is the original value undefined, and there is a situation where the undefined is a local variable, like any other ordinary variable, without any specificity, Its value is not necessarily undefined, but it is usually true. The following is what we call undefined, which refers to the window.undefined attribute.
In ES3 (before Firefox4), window.undefined is a normal property, you can completely change its value to arbitrary truth, but in ES5 (after Firefox4), window.undefined becomes a writable, A data property that is not configurable, and its value is always undefined.
The value of an uninitialized variable is undefined, and the value of a parameter variable without an incoming argument is undefined, and if a function does not return anything, the function returns undefined by default.
You can use the strict equality operator to determine whether a value is undefined:
var foo;
Console.log (foo = = undefined);//true
console.log (typeof foo = = ' undefined ');//true
Console.log (window.foo = = = undefined);//true
console.log (bar = = undefined);//uncaught Referenceerror:bar are not defined Console.log
(typeof bar = = ' undefined ');//true
console.log (Window.bar = = undefined);//true
Console.log (typeof undefined = = ' undefined '); True
Console.log (typeof null = = ' object ');//true
console.log (null = = undefined);//true
Console.log ( Null = = = undefined);//false
Summarize
The value of NULL is NULL, representing an empty object pointer that does not point to any object
The value of the undefined is undefined, which indicates that the property of the declared variable or object was not initialized
Undefined values are derived from null, so performing an equality test on them returns true
Values, Boolean values, objects, and string values all have the ToString () method. But null and undefined values do not have this method
In most cases, calling the ToString () method does not have to pass parameters. However, when you call the ToString () method of a numeric value, you can pass a parameter: the cardinality of the output value
var num = 10;
Alert (num.tostring ()); "10"
Alert (num.tostring (2)); "1010"
Alert (num.tostring (8)); "12"
Alert (num.tostring (10)); "10"
Alert (num.tostring (16)); A
You can also use the transformation function string () When you do not know if the value to convert is null or undefined, and this function can convert any type of value to a string. The String () function follows the following conversion rules:
If the value has the ToString () method, call the method (without parameters) and return the corresponding result
Returns "NULL" if the value is null
Returns "Undefined" if the value is undefined
Six, Object
Everything in JavaScript is Object
Objects
typeof {A:1} = = = = ' object ';
Use the Array.isarray or Object.prototype.toString.call method to differentiate the array types from the basic objects
typeof [1, 2, 4] = = ' object ';
typeof new Date () = = ' object ';
The following is easy to confuse, do not use this!
typeof New Boolean (true) = = = ' object ';
typeof new Number (1) = = = ' object ';
typeof New String ("abc") = = ' object ';
Functions
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 the definition property is __proto__: Value or "__proto__": Value, the name __proto__ property is not created. If the given value is an object or null, then the [[Prototype]] of the object is set to the given value. (If the given value is not an object or null, then the object's prototype will not change.) )
var obj1 = {};
ASSERT (object.getprototypeof (obj1) = = Object.prototype);
var obj2 = {__proto__: null};
ASSERT (object.getprototypeof (obj2) = = null);
var protoobj = {};
var obj3 = {"__proto__": protoobj};
ASSERT (object.getprototypeof (obj3) = = protoobj);
var obj4 = {__proto__: ' not ' an 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 stereotype are considered grammatical errors.
A property definition that does not use the colon notation does not alter the object's prototype; it is a normal property definition, just like any other property with a different name.
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 allows only "property": Attribute definitions in the form of value syntax. Property names must be enclosed in double quotes. And the attribute definition does not allow easy writing.
In JSON, the value of a property only allows strings, numbers, arrays, true,false, or other JSON objects.
In JSON, the value is not allowed to be set to a function.
Objects such as Date, which are processed by Json.parse (), become strings.
Json.parse () does not process the computed property name and is thrown as an error.
DefineProperty
The Object.defineproperty () method defines a new attribute directly on an object, or modifies an existing property and returns the object
//using __proto__
object.defineproperty (obj , ' key ', {
__proto__: null,///No inherited property
value: ' Static '//No enumerable
//No configurable
//No writable< c7/>//as the default value
});
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, configurable
//To Object Prototypes
(object.freeze| | Object) (Object.prototype);
Configurable if and only if this property descriptor value is true, the property may change, or it may be deleted from the appropriate object. The default is False.
Enumerabletrue if and only if the property appears in the corresponding object enumeration property. The default is False.
Value associated with the property. can be any valid JavaScript value (numeric, object, function, etc.). Default is undefined.
Writable true if and only if the value associated with the attribute can be changed with an assignment operator. The default is False.
The access descriptor also has the following optional key values:
Get a method that provides getter for attributes, or undefined if there is no getter. method returns the value that is used as a property. Default is undefined.
Set a method that provides a setter for an attribute, or undefined if there is no setter. This method will receive a new value that is assigned as a unique parameter to the property. Default is undefined.
Variable definitions in PS:JS
In JavaScript, you use var to define variables of any type, and each variable is just a placeholder for storing data.
var temp; This code defines a variable, but its type is unknown and can hold any type of value, and when it is not initialized, the store in test is undefined.
var temp=2; This code defines a variable and initializes it directly to a numeric type.
var temp= "JavaScript"; This code defines a variable and directly initializes the micro-string, both single and double quotes, as long as the pairs appear on the line.
2, the scope of the variable
In JavaScript, you use a variable defined by Var whose scope is within the method or function that defines the variable. In other words, variables defined with VAR are local variables.
Cases:
function test ()
{
var temp= "Hello, javascript!";
}
Test (); The variable is created and initialized when the method is called, and the variable is destroyed when the execution ends.
Alert (temp); Undefined Because the variable temp has been destroyed, it is undefined (undefined).
If you do not use Var when defining a variable, the defined variable is a global variable.
Cases:
function Test2 ()
{
temp2= "Hello, javascript!";
}
Test2 (); When a method is invoked, the variable is created and initialized, and the variable persists after execution.
The above is a small series of JavaScript to introduce the data type of the trip, I hope you like.