javascript OOP语法,以及既是变量又是函数的对象
这与我之前在这里提出的一个问题有关: 我从一段时间以来一直在使用检查答案中的方法,而且效果很好。但我想进一步修改工具包的语法javascript OOP语法,以及既是变量又是函数的对象,javascript,oop,Javascript,Oop,这与我之前在这里提出的一个问题有关: 我从一段时间以来一直在使用检查答案中的方法,而且效果很好。但我想进一步修改工具包的语法 foo(firstarg).bar(secondarg);//应与上述问题相同。 foo(onlyarg).bar//一个带有一个参数的函数 foo.bar(仅限);//当第一个参数不合适时,也应该起作用。 foo.bar;//没有参数的函数,或返回静态值。 我希望所有4个语法都使用同一个foo对象,但缺乏对OOP的理解。我已经尝试了一些方法,到目前为止,我可以让1和2起
foo(firstarg).bar(secondarg);//应与上述问题相同。
foo(onlyarg).bar//一个带有一个参数的函数
foo.bar(仅限);//当第一个参数不合适时,也应该起作用。
foo.bar;//没有参数的函数,或返回静态值。
var main = function(obj){ this.obj = obj; };
var tool = function(obj){ return new main(obj); };
main.prototype = {
alertThisPlus : function(plus){
alert(this.obj + ' ' + plus);
},
alertJustThis : function(){
return alert(this.obj);
}
};
用法
我想做的是:
tool('hello').alertThisPlus('world'); // returns alert('hello world') no change
tool.alertJustThis('hello world'); // returns alert('hello world') does not work
我能想到的唯一方法是,如果您是a)将函数作为回调参数传递,或者b)让函数自执行,那么就可以不带括号地执行函数
var foo = (function() {
alert('foo called');
})();
更进一步,假设您使用了一些闭包来返回值
var foo = (function() {
return {
bar: "arf arf said the dog"
}
})();
现在您可以使用:
foo.bar;
函数只是对象,所以您可以将函数添加到“工具”。您可以手动执行此操作:
tool.foobar = function() {};
或者,如果您的类结构合理,您可以使用混合方法。大概是这样的:
function Tool(prefix) {
this.prefix = prefix;
}
Tool.prototype.alertThisPlus = function(suffix) {
alert((typeof this.prefix != 'undefined' ? this.prefix + ' ' : '') + suffix);
};
Tool.prototype.alertJustThis = function(msg) {
alert(msg);
};
function tool(prefix) {
return new Tool(prefix);
}
// Mix-in the methods from a static instance of Tool onto the 'tool' function.
// This makes both tool.alertThisPlus() and tool.alertJustThis() available,
// both will be called in the context of 'staticTool'.
(function() {
var staticTool = new Tool();
for (var o in staticTool) {
if (typeof staticTool[o] == 'function') {
tool[o] = staticTool[o].bind(staticTool);
}
}
})();
tool('hello').alertThisPlus('world'); // returns alert('hello world')
tool().alertJustThis('hello world'); // returns alert('hello world')
tool.alertJustThis('hello world'); // returns alert('hello world')
这真的不是OOP。。。但是沉默点。3和4是不可比较的,除非
bar
是一个函数。要使它们兼容,最好的方法是调用bar
,不带任何参数。同样地,1和2.right。我已经有1个和2个工作了,我已经有3个和4个工作了。它一下子就开始工作了。我知道这是可能的,因为jquery:$(arg).foo和$.browser都可以工作。注意,Function.bind()
并不是所有浏览器都支持,所以您可能需要使用闭包或编写自己的绑定函数。除了这一部分,我什么都懂:(Function(){var staticTool=new Tool();for(staticTool中的var o){if(typeof staticTool[o]='function'){tool[o]=staticTool[o].bind(staticTool);}}}}}();你能解释一下那里发生了什么吗?它创建了tool
的静态实例,然后将每个方法复制到tool
函数上。如果你这样做了:tool[o]=staticTool[o]
它们将在全局上下文中被调用,通过使用bind,您正在将作用域绑定到staticTool
。这相当于:tool[o]=function(){staticTool[o].apply(staticTool,arguments);}
我对这段代码有一个问题,我确信这是我的理解不足。这里的行tool[o]=staticTool[o].bind(staticTool);
在ie 7和ie 8中引发错误。“对象不支持此属性或方法”知道为什么吗?我不明白.bind真正支持什么。
function Tool(prefix) {
this.prefix = prefix;
}
Tool.prototype.alertThisPlus = function(suffix) {
alert((typeof this.prefix != 'undefined' ? this.prefix + ' ' : '') + suffix);
};
Tool.prototype.alertJustThis = function(msg) {
alert(msg);
};
function tool(prefix) {
return new Tool(prefix);
}
// Mix-in the methods from a static instance of Tool onto the 'tool' function.
// This makes both tool.alertThisPlus() and tool.alertJustThis() available,
// both will be called in the context of 'staticTool'.
(function() {
var staticTool = new Tool();
for (var o in staticTool) {
if (typeof staticTool[o] == 'function') {
tool[o] = staticTool[o].bind(staticTool);
}
}
})();
tool('hello').alertThisPlus('world'); // returns alert('hello world')
tool().alertJustThis('hello world'); // returns alert('hello world')
tool.alertJustThis('hello world'); // returns alert('hello world')