var blah = (function(){
function ret(){
}
ret.prototype = Object.create(Object.prototype, {
getone: {
get: function() { return 1; }
},
funcstuff: function(){ console.log('funcstuff'); }
});
return ret;
})();
var b = new blah();
console.log(b.getone); // 1
b.funcstuff(); // Uncaught TypeError: Property 'funcstuff'
// of object #<Object> is not a function
我想知道正确的语法用于添加funcstuff
上述使用Object.create()
的ret
原型。使用的Object.create()的原型合并功能与特性
你真的想解决什么问题?为什么不只是将方法添加到blah.prototype?你为什么需要使用'Object.create()'来添加方法到原型? – jfriend00
@ jfriend00因为我想添加很多属性和方法。我不想在定义方法后执行'Object.defineProperty()'foreach属性。 – Johan
@ jfriend00:不适合'getone'的getter。 –