JS Learning notes-Standard objects

Source: Internet
Author: User

First, the object

Everything is object in JS.

Ii. Types of objects

Number, String, Boolean, Undefined, function, object, and so on

Use typeof to get the type of object

Such as:

Alert (typeof 123); Number

Alert (typeof NaN); Number

Alert (typeof ' Str '); String

Alert (typeof true); Boolean

Alert (typeof undefined); Undefined

Alert (typeof Math.Abs); function

Alert (typeof null); Object

Alert (typeof []); Object

Alert (typeof {}); Object

Third, packaging objects

1. Packaging objects

Number, Boolean, string all have wrapper objects, wrapper objects are created with new. When the object is wrapped, the value is the same, but the type becomes object.

Such as:

var num = new number (123);

var bool = new Boolean (true);

var str = new String (' abc ');

It is best not to use wrapper objects, especially string types.

2. When using number, Boolean, string, new is not used, which is only equivalent to data type conversions.

var num = number (' 123 '); 123, equivalent to parseint () or parsefloat ()

Alert (typeof num); Number

var bool = Boolean (' true '); True

Alert (typeof bool); Boolean

var bool2 = Boolean (' false '); True non-empty string is true

var bool3 = Boolean ("); False

var str = String (123.45); ' 123.45 '

Alert (typeof str); String

Iv. points to be aware of

    • Do not new Number() use new Boolean() , new String() create packaging objects;

    • Use parseInt() or parseFloat() to convert any type to number ;

    • String()A method that converts any type to string , or calls directly an object toString() (null and undefined without the method; The number object uses the ToString () method to require special handling: 123.. ToString (); or (123). ToString (););

    • It is not usually necessary to convert any type to boolean re-judgment, because it can be written directly if (myVar) {...} ;

    • typeofOperators can determine the,,, number boolean string function and undefined ;

    • Judge Array to use Array.isArray(arr) ;

    • Judging null please use myVar === null ;

    • Determine if a global variable exists typeof window.myVar === ‘undefined‘ ;

    • The function internally determines whether a variable exists typeof myVar === ‘undefined‘ .

JS Learning notes-Standard objects

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.