JavaScript Advanced (2)---object Type
JavaScript Original Object Type (6):
- (data Type)number,string,boolean,function,undefined
- (object Type)object(null, {}, Array, Date, RegExp ...) )
About Nan
- Nan is a special number type that is not equal to any numeric value, and includes his own
- The value of typeof (NaN) is number
Implicit conversions
1 var x = "66h34tyy" + ; 2 var y = + "hahaha"; 3
The above three kinds of "+" are understood as String concatenation, return is a string type of data
1 var
The "-" number here is understood to be Subtraction.
// set car to null1 car-0 //2 car + "" //car plus empty string
These two methods can be used to skillfully change the car ( do subtraction ) to number or ( do addition ) into a string
An explicit conversion
varNum=number ('123');//converts the string ' 123 ' to numberConsole.log (num+"is a"+typeof(num) +"type");//123 is number typevarBoo=boolean ('false');//converts the string ' false ' to a BooleanConsole.log (boo+"is a"+typeof(boo) +"type");//true is a Boolean type//Note that this is true only if the contents of the Boolean () conversion are NOT NULL or "" .varStr=string (123.56);//converts the string ' 123 ' to stringConsole.log (str+"is a"+typeof(str) +"type");//123.56 is a string type
- Note the type of the converted result data is the corresponding number, string, boolean
Some of the special values
null = = undefined; // returns True null = = undefined; // returns false
typeof (null// returns True because of historical reasons
Wrapping Object
var New Number (123); typeof (num); // returns "object" var New Boolean (truetypeof(boo); // returns "object" var New String (' str '); typeof(str); // returns "object"
- The value of the generated wrapper class is equal to the value created without using the wrapper class
- But the type created with the wrapper class is "object"
Only object types can have properties
- is an object type of data
- objects created using the wrapper class
vararr=[];//[] belongs to type Objectarr.t=3; Console.log (arr.t);//the bound property T can be accessed normallyvarobj={};//{} belongs to type Objectobj.t=3; Console.log (obj.t);//the bound property T can be accessed normallyvarstr_obj=NewString ("aaaaaa");//Str_obj is a string created using the wrapper class, which belongs to the object typestr_obj.t="Obj_prop"; Console.log (str_obj.t); //bound properties can also be output properlyvarStr="AAAAA";//str is of type string (note that this is not created using wrapper classes)str.t=3;//here is a temporary attribute for the STR help topConsole.log (str.t);//There's No access back here, back to Undefined.Console.log (str.length)//a special property of string type is length, which can be accessedvarnum=123;//num is of type number (note that this is not created using the wrapper Class)num.t=3;//here is a temporary property for num helpConsole.log (num.t);//There's No access back here, back to Undefined.
Type detection
instanceof
Operator is used to test whether an object has a prototype property on its prototype chain Constructor.
instanceof
operator is used to detect the constructor.prototype
existence object
of a prototype chain on a parameter.
Grammar:object instanceof constructor
[1,2true; Newfalse ;
(note: instanceof can not be used when cross-iframe, equivalent to two window objects)
typeof操作符返回一个字符串,
Indicates 经
the type of the operand that is not evaluated
- typeof can accurately determine "number", "boolean", "string", "undefined", "function" at a time
- Array needs to use the IsArray () method to determine
- Some tests for other situations
- You do not need to convert an object to a Boolean in the If () judgment statement
- In addition to null and undefined, other objects have the ToString () method, which converts the object to a string
- When the number type object calls the toString () method, It is necessary to enclose itself in a live using the double dot
// ' 123 'console.log (123..toString ()); // ' 123 '
- To determine the object "null", you need to use obj = = = null , in the form of "all equals"
JavaScript Advanced (2)---object Type