call()的用法

转自:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/call

call() 方法在使用一个指定的this值和若干个指定的参数值的前提下调用某个函数或方法.

注意:该方法的作用和  apply() 方法类似,只有一个区别,就是 call()方法接受的是 若干个参数的列表,而 apply()方法接受的是 一个包含多个参数的数组

语法

fun.call(thisArg[, arg1[, arg2[, ...]]])

参数

thisArg
fun函数运行时指定的 this需要注意的是,指定的 this值并不一定是该函数执行时真正的 this值,如果这个函数处于 非严格模式下,则指定为 nullundefinedthis值会自动指向全局对象(浏览器中就是window对象),同时值为原始值(数字,字符串,布尔值)的 this会指向该原始值的自动包装对象。
arg1, arg2, ...
指定的参数列表。

描述

当调用一个函数时,可以赋值一个不同的 this 对象。this 引用当前对象,即 call 方法的第一个参数。

通过 call 方法,你可以在一个对象上借用另一个对象上的方法,比如Object.prototype.toString.call([]),就是一个Array对象借用了Object对象上的方法。

示例

使用call方法调用父构造函数

在一个子构造函数中,你可以通过调用父构造函数的 call 方法来实现继承类似于Java中的写法。下例中,使用 Food 和 Toy 构造函数创建的对象实例都会拥有在 Product 构造函数中添加的 name 属性和 price 属性,但 category 属性是在各自的构造函数中定义的。

function Product(name, price) {
  this.name = name;
  this.price = price;

  if (price < 0) {
    throw RangeError('Cannot create product ' +
                      this.name + ' with a negative price');
  }

  return this;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

Food.prototype = Object.create(Product.prototype);
Food.prototype.constructor = Food; // Reset the constructor from Product to Food

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

Toy.prototype = Object.create(Product.prototype);
Toy.prototype.constructor = Toy; // Reset the constructor from Product to Toy

var cheese = new Food('feta', 5);
var fun = new Toy('robot', 40);

使用call方法调用匿名函数

在下例中的for循环体内,我们创建了一个匿名函数,然后通过调用该函数的call方法,将每个数组元素作为指定的this值执行了那个匿名函数。这个匿名函数的主要目的是给每个数组元素对象添加一个print方法,这个print方法可以打印出各元素在数组中的正确索引号。当然,这里不是必须得让数组元素作为this值传入那个匿名函数(普通参数就可以),目的是为了演示call的用法。

var animals = [
  {species: 'Lion', name: 'King'},
  {species: 'Whale', name: 'Fail'}
];

for (var i = 0; i < animals.length; i++) {
  (function (i) { 
    this.print = function () { 
      console.log('#' + i  + ' ' + this.species + ': ' + this.name); 
    } 
    this.print();
  }).call(animals[i], i);
}

使用call方法调用函数并且指定上下文的'this'

在下面的例子中,当调用 greet 方法的时候,该方法的 this 值会绑定到 对象。
 

function greet() {
  var reply = [this.person, 'Is An Awesome', this.role].join(' ');
  console.log(reply);
}

var i = {
  person: 'Douglas Crockford', role: 'Javascript Developer'
};

greet.call(i); // Douglas Crockford Is An Awesome Javascript Developer

补充于:2016.10.14


1、方法定义

call方法: 
语法:call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) 
定义:调用一个对象的一个方法,以另一个对象替换当前对象。 
说明: 
call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。 
如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。 

apply方法: 

语法:apply([thisObj[,argArray]]) 
定义:应用某一对象的一个方法,用另一个对象替换当前对象。 
说明: 
如果 argArray 不是一个有效的数组或者不是 arguments 对象,那么将导致一个 TypeError。 
如果没有提供 argArray 和 thisObj 任何一个参数,那么 Global 对象将被用作 thisObj, 并且无法被传递任何参数。

 

2、常用实例

a、

Java代码   收藏代码
  1. function add(a,b)  
  2. {  
  3.     alert(a+b);  
  4. }  
  5. function sub(a,b)  
  6. {  
  7.     alert(a-b);  
  8. }  
  9.   
  10. add.call(sub,3,1);   

 这个例子中的意思就是用 add 来替换 sub,add.call(sub,3,1) == add(3,1) ,所以运行结果为:alert(4); // 注意:js 中的函数其实是对象,函数名是对 Function 对象的引用。

 

b、

Java代码   收藏代码
  1. function Animal(){    
  2.     this.name = "Animal";    
  3.     this.showName = function(){    
  4.         alert(this.name);    
  5.     }    
  6. }    
  7.   
  8. function Cat(){    
  9.     this.name = "Cat";    
  10. }    
  11.    
  12. var animal = new Animal();    
  13. var cat = new Cat();    
  14.     
  15. //通过call或apply方法,将原本属于Animal对象的showName()方法交给对象cat来使用了。    
  16. //输入结果为"Cat"    
  17. animal.showName.call(cat,",");    
  18. //animal.showName.apply(cat,[]);  

 call 的意思是把 animal 的方法放到cat上执行,原来cat是没有showName() 方法,现在是把animal 的showName()方法放到 cat上来执行,所以this.name 应该是 Cat

 

c、实现继承

Java代码   收藏代码
  1. function Animal(name){      
  2.     this.name = name;      
  3.     this.showName = function(){      
  4.         alert(this.name);      
  5.     }      
  6. }      
  7.     
  8. function Cat(name){    
  9.     Animal.call(this, name);    
  10. }      
  11.     
  12. var cat = new Cat("Black Cat");     
  13. cat.showName();  

 Animal.call(this) 的意思就是使用 Animal对象代替this对象,那么 Cat中不就有Animal的所有属性和方法了吗,Cat对象就能够直接调用Animal的方法以及属性了.

 

d、多重继承

Java代码   收藏代码
  1. function Class10()  
  2. {  
  3.     this.showSub = function(a,b)  
  4.     {  
  5.         alert(a-b);  
  6.     }  
  7. }  
  8.   
  9. function Class11()  
  10. {  
  11.     this.showAdd = function(a,b)  
  12.     {  
  13.         alert(a+b);  
  14.     }  
  15. }  
  16.   
  17. function Class2()  
  18. {  
  19.     Class10.call(this);  
  20.     Class11.call(this);  
  21. }  

 很简单,使用两个 call 就实现多重继承了
当然,js的继承还有其他方法,例如使用原型链,这个不属于本文的范畴,只是在此说明call 的用法。说了call ,当然还有 apply,这两个方法基本上是一个意思,区别在于 call 的第二个参数可以是任意类型,而apply的第二个参数必须是数组,也可以是arguments
还有 callee,caller..

 

例子来源:http://xiaofeizm55333.iteye.com/blog/80913

http://www.iteye.com/topic/599108  


  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值