当我在函数内部时,有什么方法可以获取函数引用?
var arrayOfFunction = [ myFunction ];
arrayOfFunction[0]();
function myFunction() {
removeFromArray( thisFunctionReference ) // how to get function self reference here?
};
function removeFromArray(functionRef) {
index = arrayOfFunction.indexOf(functionRef);
if (index > -1) {
arrayOfFunction.splice(index, 1);
}
}
答案 0 :(得分:3)
在JavaScript中,函数是第一类成员,因此其行为类似于任何其他对象。您可以像这样通过名称来引用它:
myFunction.property = 5;
function myFunction() {
console.log(myFunction.property) //logs 5
console.log(myFunction) // logs the function
};
myFunction();
答案 1 :(得分:-1)
您也可以执行以下操作
removeFromArray(arguments.callee);
是否应该这样做取决于您是否打算在应用中使用“严格”模式。 More info in documentation。