javascript类继承自Function类 [英] javascript class inherit from Function class
问题描述
我喜欢在javascript中,我可以创建一个函数,然后为该函数添加更多方法和属性
I like that in javascript, I can create a function, and then add further methods and attributes to that function
myInstance = function() {return 5}
myInstance.attr = 10
我想要创建一个类来生成这些对象。我假设我必须从Function基类继承。
I would like to create a class to generate these objects. I assume I have to inherit from the Function base class.
换句话说,我想:
var myInstance = new myFunctionClass()
var x = myInstance()
// x == 5
但我不知道如何创建myFunctionClass。我尝试了以下方法,但它不起作用:
But I don't know how to create the myFunctionClass. I have tried the following, but it does not work:
var myFunctionClass = function() {Function.call(this, "return 5")}
myFunctionClass.prototype = new Function()
myInstance = new myFunctionClass()
myInstance()
// I would hope this would return 5, but instead I get
// TypeError: Property 'myInstance' of object #<Object> is not a function
我还尝试了更复杂(也更合适?)的继承方法: 如何正确地用JavaScript创建一个自定义对象?,没有更多运气。我也尝试使用node.js中的util.inherits(myFunctionClass,Function)。仍然没有运气
I also tried the more complicated (and more proper?) inheritance method found here: How to "properly" create a custom object in JavaScript?, with no more luck. I have also tried using the util.inherits(myFunctionClass, Function) found in node.js. Still no luck
我已经筋疲力尽了谷歌,因此我觉得我必须遗漏一些基本或明显的东西。非常感谢帮助。
I have exhausted Google, and therefore feel that I must be missing something fundamental or obvious. Help would be greatly appreciated.
推荐答案
您尝试继承功能
。这是一个正确的痛苦。我建议你改为:
Your trying to inherit from Function
. This is a right pain to do. I suggest you do the following instead
Live Example
var Proto = Object.create(Function.prototype);
Object.extend(Proto, {
constructor: function (d) {
console.log("construct, argument : ", d);
this.d = d;
// this is your constructor logic
},
call: function () {
console.log("call", this.d);
// this get's called when you invoke the "function" that is the instance
return "from call";
},
method: function () {
console.log("method");
// some method
return "return from method";
},
// some attr
attr: 42
});
您想要创建一个构成类基础的原型对象。它有你的通用方法/属性。它还有一个在对象构造时调用的构造函数和一个在调用函数时调用的调用方法
You want to create a prototype object that forms the basis of your "class". It has your generic methods/attributes. It also has a constructor that gets invoked on object construction and a call method that gets invoked when you call the function
var functionFactory = function (proto) {
return function () {
var f = function () {
return f.call.apply(f, arguments);
};
Object.keys(proto).forEach(function (key) {
f[key] = proto[key];
});
f.constructor.apply(f, arguments);
return f;
}
}
函数工厂获取原型对象并返回工厂为了它。调用时返回的函数将为您提供一个从您的原型对象继承的新函数对象。
A function factory takes a prototype object and returns a factory for it. The returned function when called will give you a new function object that "inherits" from your prototype object.
var protoFactory = functionFactory(proto);
var instance = protoFactory();
在此创建工厂,然后创建实例。
Here you create your factory and then create your instance.
然而,这不是适当的原型OO。我们只是将原型的属性浅层复制到一个新对象中。因此对原型的更改不会反映回原始对象。
However this isn't proper prototypical OO. we are just shallow copying properties of a prototype into a new object. So changes to the prototype will not reflect back to the original object.
如果你想要真正的原型OO,那么你需要使用hack。
If you want real prototypical OO then you need to use a hack.
var f = function () {
// your logic here
};
f.__proto__ = Proto;
注意我们如何使用非标准弃用 .__ proto __
和我们在运行时改变了 [[Prototype]]
的值,这被认为是邪恶的。
Notice how we use the non-standard deprecated .__proto__
and we are mutating the value of [[Prototype]]
at run-time which is considered evil.
这篇关于javascript类继承自Function类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!