Details about passing parameters in newObject in js _ javascript skills

Source: Internet
Author: User
Here we will discuss the internal processing of passing parameters to an Object. Reference: ECMA262V515.2.2.1 1. A parameter is an object. The core js object (native ECMAScript object) or host object will be returned directly.
The object constructor generated by the constructor is still the constructor of the passed parameter object. The consequence is that although the Object is a new Object, its constructor is not necessarily an Object.

The Code is 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. A parameter is a basic type object, such as a String, Number, or Boolean, which is encapsulated into an object (converted to its corresponding packaging class).

The Code is 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


It can be seen from the above that when passing a parameter, the Object generated using the new Object's constructor does not necessarily point to the Object, and will point to the Object only when it is very clever, such

The Code is 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.

The Code is 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

Related Article

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.