在javascript片段中,我看到了这样的代码..
nglr.shiftBind = function(_this, _function) {
return function() {
var args = [ this ];
for ( var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
return _function.apply(_this, args);
};
};
这看起来有点抽象让我理解......任何人都可以有一个关于这个真正做什么的例子......
谢谢!
答案 0 :(得分:2)
这似乎将当前范围(当您调用返回的函数时)添加到传递给_function
的任何参数,然后最终使用指定范围(_this
)调用它。
// Take a new scope and a callback
var shiftBind = function(_this, _function) {
// Wrap them, creating closure
return function() {
// Save the current scope, when you call this function
var args = [this];
// Prepend it to any arguments
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
// Then call the original function with the original scope argument
return _function.apply(_this, args);
};
};
// Test it
function Ctor(foo) {
this.foo = foo;
}
// Make a pair of Ctors and give x a method
var x = new Ctor(1);
var y = new Ctor(3);
x.getFoo = function(prev) {
return prev ? prev.foo + this.foo : this.foo;
}
// Check outputs
console.log(x.getFoo()); // 1
console.log(x.getFoo.call(y)); // 3
// Bind so we have access to both "scopes"
x.boundFoo = shiftBind(y, x.getFoo);
console.log(x.boundFoo()); // calls getFoo, which only exists on x, but prints 4
&#13;
这似乎没有大量的实际用途,并且我之前看不到它的模式,但允许您访问具有this
的对象 - 就像语义一样。
答案 1 :(得分:1)
这是创建一个新函数,用于更改原始函数的this
,但添加原始this
作为第一个参数。所以它既有约束力又可以将论点向下移动一个。我不知道它的目的是什么。
答案 2 :(得分:1)
此功能可以定义要传递给函数的参数和参数类型(字符串,数字......)。例如:
var nglr = {};
nglr.shiftBind = function(_this, _function) {
return function() {
var args = [ this ];
for ( var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
return _function.apply(_this, args);
};
};
var obj2 = new Object();
obj2.name = '';
// 1. defined a function that is called when the returned function is executed.
var sumFunc = function(obj, param, param2, param3, param4, param5){
console.log('obj2: ', this); // obj2
console.log(obj, param, param2, param3, param4, param5); // Window-Object 1 2 3 4 5
return param + param2 + param3 + param4 + param5;
};
// 2. hold returned function at variable
var returnedFunc = nglr.shiftBind(obj2, sumFunc);
// 3. call returned function, now all parameters that were given to the returned function are passed on to the sumFunc-function plus as first param the object which context the returned function is executed in(in this case Window, because returnedFunc is not called as member-function).
returnedFunc(1,2,3,4,5); // 15
这就是为什么当你将第五个参数传递给返回的函数但你只定义了4个参数加上obj参数时,如下所示:
var sumFunc = function(obj, param, param2, param3, param4){
console.log(obj, param, param2, param3, param4); // Window-Object 1 2 3 4
return param + param2 + param3 + param4;
};
它返回10而不是15:
returnedFunc(1,2,3,4,5); // 10
尽管如此,你可以在arguments-property(在console.log中)看到第五个参数(5),但它并没有被定义为sumFunc函数中的第六个参数。因此返回10而不是15:
var nglr = {};
nglr.shiftBind = function(_this, _function) {
return function() {
var args = [ this ];
console.log(arguments); // [1, 2, 3, 4, 5]
for ( var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
return _function.apply(_this, args);
};
};
此外,shiftBind意味着当调用sumFunc时,“this”属性可能是一个不同的对象。在这个例子中obj2而不是nglr:
var sumFunc = function(obj, param, param2, param3, param4, param5){
console.log('obj2: ', this); // obj2 not nglr
//...
};