如何创建Javascript的子类';s函数类?
我想创建一个Javascript类,我可以这样使用:如何创建Javascript的子类';s函数类?,javascript,inheritance,function,oop,Javascript,Inheritance,Function,Oop,我想创建一个Javascript类,我可以这样使用: var f = new myFunction(arg1, arg2); f.arg1; // -> whatever was passed as arg1 in the constructor f(); f instanceof myFunction // -> true typeof f // -> function 我可以像对待普通对象一样对待它,甚至可以将本机函数对象添加到原型链中,但我不能像调用函数一样调用它: f
var f = new myFunction(arg1, arg2);
f.arg1; // -> whatever was passed as arg1 in the constructor
f();
f instanceof myFunction // -> true
typeof f // -> function
我可以像对待普通对象一样对待它,甚至可以将本机函数
对象添加到原型链中,但我不能像调用函数一样调用它:
function myFunction(arg1, arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
}
myFunction.prototype = Function
var f = new myFunction(arg1, arg2); // ok
f.arg1; // ok
f(); // TypeError: f is not a function, it is object.
f instanceof myFunction // -> true
typeof f // -> object
我可以让构造函数返回一个函数,但它不是myFunction
的实例:
function myFunction(arg1, arg2) {
var anonFunction = function() {
};
anonFunction.arg1 = arg1;
anonFunction.arg2 = arg2;
return anonFunction;
}
var f = new myFunction(arg1, arg2); // ok
f.arg1; // ok
f(); // ok
f instanceof myFunction // -> false
typeof f // -> function
有什么建议吗?我应该补充一点,我确实希望避免使用
新函数()
,因为我讨厌字符串代码块。这是不可能的。如果它应该是函数的一个实例,那么它必须是一个对象。为什么要这样?函数Foo(){var o=function(){return“Foo”};o.。\uuuu proto\uuu=Foo.prototype;return o;}
(新的Foo())Foo的实例
:true
(new Foo())()
:Foo首先,您可能应该考虑其他方法,因为它不太可能是可移植的。我将假设Rhino是我的答案
其次,我不知道Javascript中有任何方法可以在构造后分配函数体。在构造对象时始终指定主体:
// Normal function definitions
function doSomething() { return 3 };
var doSomethingElse = function() { return 6; };
// Creates an anonymous function with an empty body
var doSomethingWeird = new Function;
现在,每个对象上都有一个非标准的Mozilla扩展,其形式为\uuuu proto\uuu
属性。这允许您更改任何对象的继承链。您可以将其应用于函数对象,以便在构建后为其提供不同的原型:
// Let's define a simple prototype for our special set of functions:
var OddFunction = {
foobar: 3
};
// Now we want a real function with this prototype as it's parent.
// Create a regular function first:
var doSomething = function() {
return: 9;
};
// And then, change it's prototype
doSomething.__proto__ = OddFunction;
// It now has the 'foobar' attribute!
doSomething.foobar; // => 3
// And is still callable too!
doSomething(); // => 9
// And some of the output from your example:
doSomething instanceof OddFunction; // => true
typeof doSomething; // => function
我不知道你为什么要这样做。但这里有一个片段很接近
function MyFunction(arg1, arg2)
{
this.firstProp = arg1;
this.secondProp = arg2;
}
var f = function(arg1, arg2) {
return new MyFunction(arg1, arg2);
}(12,13);
alert("Arg1 " + f.firstProp) // 12;
alert("Arg2 " + f.secondProp) // 13;
alert(f instanceof MyFunction) // true;
我认为这是一种更具javascript风格的方法,除了它不使用instanceof作为对象,而是使用一个内部的instanceof
var f = function(arg1, arg2){
return {
instanceOf:arguments.callee,
arg1:arg1,
arg2:arg2
};
};
var fn = f(1, function(p){ alert(p); });
fn.arg1; // 1
fn.instanceOf === f; //true
typeof f; //function
typeof fn; //object
fn.arg2('hello'); //show alert hello
调用函数时会发生什么?你在哪里以及如何提供它的身体?你的目的是什么?闭包在Javascript上运行良好。我将使用第二种解决方案,并避免使用instanceof。这很容易让我说,因为我考虑代码的气味。无论如何,我想模仿斯卡拉的<代码>部分函数< /代码>。我想要mimick Scala的<代码>部分函数< />代码。我曾希望这是可能的,因为函数是Javascript中的对象,但它们似乎有一些神奇的特殊性,这超出了您可以操作的普通对象的范围。是的,“匿名”示例实际上起作用(没有想到)。但是它使用的是你不应该使用的privats。所以我强烈建议不要这样做。原型语言只是通过改变原型来实现的。是的,我知道。我说的是“原始”财产。