var Person = function()
{
function Person(name,age){
if(!(this instanceof Person))
throw Error(“Must Use NEW”);
this.name = age;
this.age = age;
}
_prototype(Person,[{key:“saynothing”, value:function saynothing()}
{
},{}])
}()
var = New Person(“11”,11);
//为什么不直接用prototype。name = {};
因为这样无法直接继承其enumerable等特性。
_prototype(Constructor.prototype,prototype, staticprototype)
{
//(construcor.prototype, prototype)
for(var i in prototype)
var descriptor = prototype[i];
decscriptor.enumerable = descriptor.enumerable||false
descriptor.configurable = true;
//当get,set函数的时候是没有value的
if(“value” in descriptor)
descriptor.writable = true;
Object.defineProperties( Constructor.prototype, descriptor.key,
descriptor)
//(constructor, staticprototype)
}
本文探讨了JavaScript中构造函数Person的实现方式,解释了为何不直接使用prototype属性,而通过自定义_prototype方法来为构造函数添加方法。讨论了这种方法如何避免直接修改prototype属性可能导致的enumerable等特性的丢失。
454

被折叠的 条评论
为什么被折叠?



