【发布时间】:2011-07-10 10:48:32
【问题描述】:
有没有办法创建一个从另一个对象继承属性的函数/可调用对象? __proto__ 可以做到这一点,但该属性已被弃用/非标准。有没有符合标准的方法来做到这一点?
/* A constructor for the object that will host the inheritable properties */
var CallablePrototype = function () {};
CallablePrototype.prototype = Function.prototype;
var callablePrototype = new CallablePrototype;
callablePrototype.hello = function () {
console.log("hello world");
};
/* Our callable "object" */
var callableObject = function () {
console.log("object called");
};
callableObject.__proto__ = callablePrototype;
callableObject(); // "object called"
callableObject.hello(); // "hello world"
callableObject.hasOwnProperty("hello") // false
【问题讨论】:
标签: javascript