【发布时间】:2011-11-24 05:54:23
【问题描述】:
// Don't break the function prototype.
// pd - https://github.com/Raynos/pd
var proto = Object.create(Function.prototype, pd({
"prop": 42
}));
var f = function() { return "is a function"; };
f.__proto__ = proto;
console.log(f.hasOwnProperty("prop")); // false
console.log(f.prop); // 42
console.log(f()); // "is a function"
.__proto__ 是非标准且已弃用。
我应该如何继承原型创建一个对象但让该对象成为一个函数。
Object.create 返回一个对象而不是函数。
new Constructor 返回一个对象而不是函数。
动机: - 跨浏览器 finherit
var finherit = function (parent, child) {
var f = function() {
parent.apply(this, arguments);
child.apply(this, arguments);
};
f.__proto__ = parent;
Object.keys(child).forEach(function _copy(key) {
f[key] = child[key];
});
return f;
};
我不相信这是可能的,所以我们可能应该向 es-discuss 邮件列表提出Function.create
/*
Creates a new function whose prototype is proto.
The function body is the same as the function fbody.
The hash of propertydescriptors props is passed to defineproperties just like
Object.create does.
*/
Function.create = (function() {
var functionBody = function _getFunctionBody(f) {
return f.toString().replace(/.+\{/, "").replace(/\}$/, "");
};
var letters = "abcdefghijklmnopqrstuvwxyz".split("");
return function _create(proto, fbody, props) {
var parameters = letters.slice(0, fbody.length);
parameters.push(functionBody(fbody));
var f = Function.apply(this, parameters);
f.__proto__ = proto;
Object.defineProperties(f, props);
return f;
};
})();
正如 es-discuss 线程中提到的,存在一个 ES:strawman <| 原型运算符,它允许这样做。
让我们看看使用<|会是什么样子
var f1 = function () {
console.log("do things");
};
f1.method = function() { return 42; };
var f2 = f1 <| function () {
super();
console.log("do more things");
}
console.log(f1.isPrototypeOf(f2)); // true
console.log(f2()); // do things do more things
console.log(f2.hasOwnProperty("method")); // false
console.log(f2.method()); // 42
【问题讨论】:
-
@patrick_dw 并没有什么帮助,但谢谢你;)
-
@O_._O 改回你的名字 ¬_¬
-
@patrick_dw 你个白痴。 o/
-
请告诉我,您现在还没有弄清楚!
-
哦,天哪。不久前,在阅读有关对象文字语法的提议扩展时,我已经看到了 prototype for 运算符,但并没有密切关注。 ES5 很好,但是现在提出了很多很酷的东西,我只是希望他们能把它搞出来,不要让我们等太久。
标签: javascript oop function inheritance prototypal-inheritance