Refer to javascript function from within itself

后端 未结 12 897
失恋的感觉
失恋的感觉 2020-12-24 06:08

Consider this piece of code

var crazy = function() {
    console.log(this);
    console.log(this.isCrazy); // wrong.
}
crazy.isCrazy = \'totally\';
crazy();
         


        
12条回答
  •  温柔的废话
    2020-12-24 06:32

    I think you are asking for arguments.callee, but it's deprecated now.

    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
    

提交回复
热议问题