考虑这段代码

var crazy = function() {
    console.log(this);
    console.log(this.isCrazy); // wrong.
}
crazy.isCrazy = 'totally';
crazy();
// ouput =>
// DOMWindow
// undefined

从crazy()内部开始,“this”是指窗口,我想这是有道理的,因为通常您希望此窗口引用该函数所附加的对象,但是如何获得该函数以引用其自身并访问本身设置的属性?

答案:

不要使用arguments.callee,而要使用命名函数。

“注意:您应该避免使用arguments.callee(),而应为每个函数(表达式)命名。”通过MDN article on arguments.callee

最佳答案

我认为您正在要求arguments.callee,但现在已弃用

https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments/callee

var crazy = function() {
    console.log(this);
    console.log(arguments.callee.isCrazy); // right.
}
crazy.isCrazy = 'totally';
crazy();
// ouput =>
// DOMWindow
// totally

08-05 15:24