Why invoke “apply” instead of calling function directly?

后端 未结 5 1052
清歌不尽
清歌不尽 2020-12-31 09:56

When looking at the source code for raphael or g.raphael or other libraries I\'ve noticed the developer does something like this:

var val = Math.max.apply(Ma         


        
5条回答
  •  心在旅途
    2020-12-31 10:36

    Why invoke “apply” instead of calling function directly? Let's have a snippet:

    var obj = {a: "apply"};
    func.call(obj, "parameter");
    function func(para) {
        if(this.a === "apply"){
            console.log('apply'); 
         }
    } 
    

    Here we can see we are using 'this' (context) within a function, If we directly call a func than we don't have any context, than it will take window context, which is wrong. So, if you are using context within your function than use apply method.

提交回复
热议问题