这里讨论给Object传参时,其内部的处理。参考:ECMA262 V5 15.2.2.1
1, 参数是一个对象,核心js对象(native ECMAScript object)或宿主对象(host object),那么将直接返回该对象。
其生成的对象构造器仍然是所传参数对象的构造器。这样造成的后果是虽然该对象是new Object,但其constructor不一定是Object。
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, 参数是基本类型对象,如字符串(String),数字(Number),布尔值(Boolean),将其包装成对象(转换成其对应的包装类)后返回。
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
从以上看出,当传参数时,使用new Object生成的对象其构造器不一定指向Object,只有很巧的时候会指向Object,如:
var obj1 = new Object,
obj2 = {};
var o1 = new Object(obj1);
o2 = new Object(obj2);
console.log(o1.constructor); //-> Object
console.log(o2.constructor); //-> Object
以上就能明了为何jquery1.4中以下代码返回false了
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
分享到:
相关推荐
1, 参数是一个对象,核心js对象(native ECMAScript object)或宿主对象(host object),那么将...} var w = new Object(window), d = new Object(document), p = new Object(new Person()); console.log(w.constructor)
Design Patterns Explained A New Perspective on Object Oriented Design 设计模式的解释,比如,工厂模式,观察者模式的等等,简单易解
Design Patterns Explained A New Perspective on Object Oriented Design 2nd Edition 这本就是《设计模式解析》(2nd)的英文版
Design Patterns Explained: A New Perspective on Object-Oriented Design By Alan Shalloway, James R. Trott Publisher : Addison Wesley Pub Date : July 09, 2001 ISBN : 0-201-...
如下所示: 代码如下: var person = new Object(); person.name = “Nicholas”; person.age = “29” person.job = “Software Engineer”; person.sayName = function () { alert(this.name); }; person....
类设计的注意事项、设计模式、内部类、Object介绍、异常细节、包(package)
The goal of this article is to review the state-of-the-art tracking methods, classify them into different categories,and identify new trends. Object tracking, in general, is a challenging problem. ...
System.Diagnostics.Process pExecuteEXE = new System.Diagnostics.Process(); pExecuteEXE.StartInfo.FileName = @"E:\Delphi.exe"; pExecuteEXE.StartInfo.Arguments = "'paramstr1 paramstr2,paramstr3'"; ...
About This Book This book has been updated to cover all the new object-oriented features introduced in ECMAScript 6 It makes object-oriented programming accessible and understandable to web ...
一个关于Unity的ScriptableObject类的简要小例子,作为记录!
Object-Oriented Programming with Object-C Object-C 进阶书籍---面向对象
在vb中利用file system object (fso)建立文件和文件夹
Inside C++ Object Model/Inside C++ Object Model/Inside C++ Object Model/Inside C++ Object Model
Delphi读写操作JSON字符串时,需引用一个superobject.pas文件。 var jo,jt:ISuperObject; begin jo:=SO(); jt:=SO(); jo.S['姓名']:='小王'; jo.I['年龄']:=25; jo.S['性别']:='男'; jt.O['人员']:=jo; ...
ldap提示object class violation
本文是对js中的onclick事件传参进行了详细的讲解,需要的朋友可以过来参考下,希望对大家有所帮助
Practical Object-Oriented Design With UML - McGraw-Hil Practical Object-Oriented Design With UML - McGraw-Hil