call()与appy()

摘自http://www.w3school.com.cn

Call()方法

call() 方法是与经典的对象冒充方法最相似的方法。它的第一个参数用作 this 的对象。其他参数都直接传递给函数自身。例如:

   1:  function sayColor(sPrefix,sSuffix) {
   2:      alert(sPrefix + this.color + sSuffix);
   3:  };
   4:   
   5:  var obj = new Object();
   6:  obj.color = "blue";
   7:   
   8:  sayColor.call(obj, "The color is ", "a very nice color indeed.");

Apply()方法

apply() 方法有两个参数,用作 this 的对象和要传递给函数的参数的数组。例如:

   1:  function sayColor(sPrefix,sSuffix) {
   2:      alert(sPrefix + this.color + sSuffix);
   3:  };
   4:   
   5:  var obj = new Object();
   6:  obj.color = "blue";
   7:   
   8:  sayColor.apply(obj, new Array("The color is ", "a very nice color indeed."));
posted @ 2011-05-28 03:07  FrankFang  阅读(335)  评论(0编辑  收藏  举报