Home  >  Article  >  Web Front-end  >  Some detailed analysis of parameters passed in new Object in js_javascript skills

Some detailed analysis of parameters passed in new Object in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:09:231004browse
1, if the parameter is an object, core js object (native ECMAScript object) or host object (host object), then the object will be returned directly.
The object constructor it generates is still the constructor of the passed parameter object. The consequence of this is that although the object is new Object, its constructor is not necessarily Object.
Copy code 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, the parameter is a basic type object, such as a string (String), a number (Number), a Boolean value (Boolean), which is packaged into an object (converted into its corresponding packaging class) and returned .
Copy code 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

As can be seen from the above, when When passing parameters, the constructor of the object generated using new Object does not necessarily point to Object. It will point to Object only by chance, such as
Copy code 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 can explain why the following code in jquery1.4 returns false
Copy the code 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
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn