js对象创建过程【来自互联网】
【本文来自互联网】【目的是个人收藏】【欢迎大家指正错误】
js中只有函数对象具备类的概念,因此要创建一个对象,必须使用函数对象。 js中只有函数对象具备类的概念,因此要创建一个对象,必须使用函数对象。函数对象内部有]方法和]方法,]用于构造对象,]用于函数调用,只有使用new操作符时才触发]逻辑。 var obj=new object(); 是使用内置的object这个函数对象创建实例化对象obj。var obj={};和var obj=;这种代码将由js引擎触发object和array的构造过程。function fn(){}; var myobj=new fn();是使用用户定义的类型创建实例化对象。
new fn(args)的创建过程如下(即函数对象的]方法处理逻辑,对象的创建过程)。另外函数对象本身的创建过程(指定义函数或者用function创建一个函数对象等方式)虽然也使用了下面的处理逻辑,但有特殊的地方,后面再描述。
- 创建一个build-in object对象obj并初始化
- 如果fn.prototype是object类型,则将obj的内部]设置为fn.prototype,否则obj的]将为其初始化值(即object.prototype)
- 将obj作为this,使用args参数调用fn的内部]方法 3.1 内部]方法创建当前执行上下文 3.2 调用f的函数体 3.3 销毁当前的执行上下文 3.4 返回f函数体的返回值,如果f的函数体没有返回值则返回undefined
- 如果]的返回值是object类型,则返回这个值,否则返回obj 注意步骤2中, prototype指对象显示的prototype属性,而]则代表对象内部prototype属性(隐式的)。 构成对象prototype链的是内部隐式的],而并非对象显示的prototype属性。显示的prototype只有在函数对象上才有意义,从上面的创建过程可以看到,函数的prototype被赋给派生对象隐式]属性,这样根据prototype规则,派生对象和函数的prototype对象之间才存在属性、方法的继承/共享关系。
用代码来做一些验证:
//passed in ff2.0, ie7, opera9.25, safari3.0.4
function fn(){}
//the value of implicit ] property of those objects derived from fn will be assigned to fn.prototype
fn.prototype={ attr1:"aaa", attr2:"bbb"};
var obj=new fn();
document.write(obj.attr1 + ""); //result: aaa
document.write(obj.attr2 + ""); //result: bbb
document.write(obj instanceof fn); //result: true
document.write("");
//i change the prototype of fn here, so by the algorithm of prototype the obj is no longer the instance of fn,
//but this won’t affect the obj and its ] property, and the obj still has attr1 and attr2 properties
fn.prototype={};
document.write(obj.attr1 + ""); //result: aaa
document.write(obj.attr2 + ""); //result: bbb
document.write(obj instanceof fn); //result: false关于创建过程返回值的验证:
//passed in ff2.0, ie7, opera9.25, safari3.0.4
function fn(){
//according to step 4 described above,
//the new fn() operation will return the object { attr1: 111, attr2: 222 }, it’s not an instance of fn!
return { attr1: 111, attr2: 222 };
}
fn.prototype={ attr1:"aaa", attr2:"bbb"};
var obj=new fn();
document.write(obj.attr1 + ""); //result: 111
document.write(obj.attr2 + ""); //result: 222
document.write(obj instanceof fn); //result: false