我有一个非常基本的问题,我找不到答案,我确实检查了mootools网站,但我还没有完全掌握这个概念:
在mootools中你有一个函数.bind(),它可以按如下方式使用:
function customFunction () {
this.setStyle('color','blue');
}
var boundFunction = customFunction.bind(myElement);
boundFunction(); //Now the color of the element is changed to red
// To show how bind works the following example:
var myBoundFunction = myFunction.bind(anyVar);
// is roughly equivalent with
var myBoundFunction = function(){
return myFunction.call(this);
};
Run Code Online (Sandbox Code Playgroud)
但为什么不这样做呢?
function customFunction (parameter) {
parameter.setStyle('color','blue');
}
customFunction(myElement);
Run Code Online (Sandbox Code Playgroud)
这似乎更有效率?
我的一个大问题是:使用.bind()会是一个好习惯,为什么会这样?