Does it make sense to use .apply( ) and pass the same instance as context?

后端 未结 2 1048
天涯浪人
天涯浪人 2021-01-22 10:09

I\'m reading Javascript Web Applications, from O\'Reilly. At various points in the book, the author uses something along the following:

instance.init.apply(insta         


        
2条回答
  •  伪装坚强ぢ
    2021-01-22 10:34

    Using apply() in that example insures that 'this' === instance, instead of DOMWindow if instance.init() is executed from another function/expression.

    var x = function(){ debugger; },
        y = function(){ x.apply(x, arguments); },
        z = function() { x(arguments); };
    
    y("abc", true, []); // this === x
    z("abc", true, []); // this === DOMWindow
    

    It's simply specifying context.

提交回复
热议问题