Javascript 我可以用函数重载对象吗?
假设我有一个函数/值的对象。我对基于调用行为的重载感兴趣 例如,下面的这段代码演示了我希望执行的操作Javascript 我可以用函数重载对象吗?,javascript,Javascript,假设我有一个函数/值的对象。我对基于调用行为的重载感兴趣 例如,下面的这段代码演示了我希望执行的操作 var main_thing = { initalized: false, something: "Hallo, welt!", something_else: [123,456,789], load: { sub1 : function() { //Some stuff }, sub2
var main_thing = {
initalized: false,
something: "Hallo, welt!",
something_else: [123,456,789],
load: {
sub1 : function() {
//Some stuff
},
sub2 : function() {
//Some stuff
},
all : function() {
this.sub1();
this.sub2();
}
}
init: function () {
this.initalized=true;
this.something="Hello, world!";
this.something_else = [0,0,0];
this.load(); //I want this to call this.load.all() instead.
}
}
我的问题是main\u thing.load
被分配给一个对象,调用main\u thing.load.all()
将调用对象内部的函数(()
操作符)。如何设置代码,以便使用main\u thing.load
访问对象,以及main\u thing.load()
执行一些代码?或者至少,类似的行为
基本上,这类似于其他语言中的默认构造函数,您不需要调用main_thing.constructor()
如果不可能,请详细解释。因为函数对象只是对象,所以引用函数的对象属性与引用普通对象的对象属性之间没有真正的区别。因此,“负载”只是外部对象的一个属性 您可以做的是初始化“init”函数中的“load”对象,使其函数可以通过闭包访问外部对象引用:
init: function() {
// whatever ...
var main_thing = this;
this.load.sub1 = function() {
main_thing.foo = "bar";
};
this.load.sub2 = function() {
main_thing.somethingElse();
}
}
现在,“load”子对象中的那些函数可以访问“main_thing”局部变量,它将引用外部对象。如何调用它们并不重要
另一种方法是在较新的浏览器中使用“bind()”功能,或者由类似于库的Functional原型提供。(就我个人而言,我只是从Functional.js中偷了bind,因为它是一个很好的干净实现。):
这种方法确保无论如何调用“sub1”,它总是将
这个绑定到定义外部对象(“sub1”)时可用的外部对象引用。您不能像这样重载javascript。
如果将main_thing.load设为函数,则可以调用main_thing.load(),还可以访问内部值,如下所示:
main_thing.load.a = 7;
main_thing.load.b = "text";
main_thing.load.foo = function(x,y){alert("Hello, world "+x+", "+y); };
main_thing.load.foo(main_thing.load.a,main_thing.load.b);
提醒“你好,世界7号文本”
但是main_thing.load本身既可以用来存储函数,也可以用来存储其他一些数据,但不能两者兼而有之。正如Tom Tu所说,函数是对象,可以具有属性
var main_thing = {
// load will be set to the result of this anonymous function
// which is a function with 2 extra properties set for the other functions
load: function() {
// create what will be the load() function and store in "all"
var all = function () {
// When the function is actually executed these will have been assigned
all.load1();
all.load2();
};
// set 2 properties for sub load functions
all.load1 = function() {};
all.load2 = function() {};
// return our function
return all;
}()
}
main_thing.load();
// or
main_thing.load.load1();
main_thing.load.load2();
您所描述的内容听起来像一个构造函数,但您将其描述为重载。我没有看到任何函数重载。函数是对象,所以您也可以将“子函数”设置为函数的属性。顺便说一句,main_-thing.load.all
中的this
并不是指main_-thing.load
而是指main_-thing.load
。我可能有点误解-但是如果您添加一个var loadfunctions={sub1:function(){..},sub2…},然后将您的加载重写为类似load:function(switch){if(!switch)的东西会怎么样{loadfunctions.all;}其他的etc@Ned它并不总是需要一个构造函数,只是一个被调用的函数。构造函数是在对象构造时被调用的。这与它类似,但并不完全相同,因为它与构造无关。@Felix感谢你指出这一点。@Prescott我不确定我是否理解你的意思,抱歉。根本不解决这个问题。他希望把Foo.a作为数据处理,把Foo.a()作为一个可能工作的函数处理,@Raynos,但我没有弄乱任何ES5代码(或Mozilla JavaScript中的等效工具)。写一个这样的答案,我会投赞成票:-)另外,@Raynos,我认为它至少解决了一些OP问题:他想调用main_thing.load.sub1()
但是要将这个
绑定到main\u thing
而不是main\u thing.load
。如果没有编译器,我所提到的是不可能的。你的JS+1将此作为我的第二次尝试。我相信这是最接近解决这个问题的方法。
var main_thing = {
// load will be set to the result of this anonymous function
// which is a function with 2 extra properties set for the other functions
load: function() {
// create what will be the load() function and store in "all"
var all = function () {
// When the function is actually executed these will have been assigned
all.load1();
all.load2();
};
// set 2 properties for sub load functions
all.load1 = function() {};
all.load2 = function() {};
// return our function
return all;
}()
}
main_thing.load();
// or
main_thing.load.load1();
main_thing.load.load2();