Refer to javascript function from within itself

后端 未结 12 861
失恋的感觉
失恋的感觉 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:42

    Easiest way to make the function itself available in its body is to do var crazy = function crazy2() { crazy2(); }, it's okay for crazy and crazy2 to have the same name since the first occurrence is the name in the outer scope and the second is the name in the function body.

    Or simply do function crazy() { crazy(); } which will define crazy in both scopes.

提交回复
热议问题