Some details of the new object Shishun in JS _javascript tips

Source: Internet
Author: User
1, the parameter is an object, a core JS object (native ECMAScript object) or a host object (host objects), then the object is returned directly.
The object builder it generates is still the constructor of the passed Parameter object. The result is that although the object is new, its constructor is not necessarily object.
Copy Code code as follows:

function person () {this.name= ' Jack ';}
var w = new Object (window),
D = new Object (document),
p = new Object (new Person ());

Console.log (W.constructor); -> Window
Console.log (D.constructor); -> HTMLDocument
Console.log (P.constructor); -> person

2, the parameter is a base type object, such as a string, a number, a Boolean value (Boolean), which is wrapped as an object (converted to its corresponding wrapper class) and returned.
Copy Code code as follows:

var s = new Object (' Hello '),
n = new Object (22),
b = new Object (true);

Console.log (typeof s); -> Object
Console.log (typeof N); -> Object
Console.log (typeof B); -> Object

Console.log (S.constructor); -> String
Console.log (N.constructor); -> number
Console.log (B.constructor); -> Boolean

As you can see from the above, when you pass a parameter, the object that you build with the new object doesn't necessarily point to object, and only when it's funny, it points to object, like
Copy Code code as follows:

var obj1 = new Object,
Obj2 = {};
var O1 = new Object (OBJ1);
O2 = new Object (OBJ2);

Console.log (O1.constructor); -> Object
Console.log (O2.constructor); -> Object

The above shows why the following code in jquery1.4+ returns false
Copy Code code as follows:

function person () {this.name= ' Jack ';}
var p = new person ();
$.isplainobject (New Object (4)); -> false
$.isplainobject (New Object (' Hello ')); -> false
$.isplainobject (New Object (true)); -> false
$.isplainobject (New Object (p)); -> false

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.