Refer to javascript function from within itself

后端 未结 12 893
失恋的感觉
失恋的感觉 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 06:39

    This has to deal with the scope of the function crazy. If can pass any scope to a function using the function call().

    Instead of

    crazy();
    

    Use

    crazy.call(crazy);
    

    For details refer
    http://odetocode.com/blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx
    https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Call
    http://devlicio.us/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx

提交回复
热议问题