JavaScript Advanced (2)---object Type

Source: Internet
Author: User
Tags wrapper

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

    1. is an object type of data
    2. 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

    • instanceofOperator 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
    1. typeof can accurately determine "number", "boolean", "string", "undefined", "function" at a time
    2. Array needs to use the IsArray () method to determine
    • Some tests for other situations
    1. You do not need to convert an object to a Boolean in the If () judgment statement
    2. In addition to null and undefined, other objects have the ToString () method, which converts the object to a string
    3. When the number type object calls the toString () method, It is necessary to enclose itself in a live using the double dot
    4. // ' 123 'console.log (123..toString ());   // ' 123 '

    5. To determine the object "null", you need to use obj = = = null , in the form of "all equals"

JavaScript Advanced (2)---object Type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.