綁定功能sets
......
sets.id = index;
grid.hex[poly].click($.proxy(function(e){
var id = this.id;
}, sets);
.....
或者,如果你不使用jQuery。
//https://gist.github.com/978329
if(!Function.prototype.bind) {
Function.prototype.bind = function(to){
// Make an array of our arguments, starting from second argument
var partial = Array.prototype.splice.call(arguments, 1),
// We'll need the original function.
fn = this;
var bound = function(){
// Join the already applied arguments to the now called ones (after converting to an array again).
var args = partial.concat(Array.prototype.splice.call(arguments, 0));
// If not being called as a constructor
if (!(this instanceof bound)){
// return the result of the function called bound to target and partially applied.
return fn.apply(to, args);
}
// If being called as a constructor, apply the function bound to self.
fn.apply(this, args);
}
// Attach the prototype of the function to our newly created function.
bound.prototype = fn.prototype;
return bound;
}
grid.hex[poly].click(function(e){
var id = this.id;
}.bind(sets));
由於關閉,處理程序裏面沒有'index'嗎? – David
這是一個函數裏面,集合在一個數組裏面,並且有很多不相關的東西我沒有包含。我不能像原樣使用索引。 –
'var id = sets.id'?這是你在找什麼? – bfavaretto