摘自:http://blog.csdn.net/chelen_jak/article/details/21021101

在web前端开发过程中,我们经常需要改变this指向,通常我们想到的就是用call方法

一、call方法的定义

关于call的定义都很拗口。在我的理解,a.call(b,arg1,arg2..)就是a对象的方法应用到b对象上。例如如下例子:

function add(a,b)
{
alert(a+b);
}
function reduce(a,b)
{
alert(a-b);
}
add.call(reduce,1,3) //将add方法运用到reduce,结果为4

二、call可以改变this指向

如下例:

function b()
{
alert(this)
}
b(); //window
b.call(); //window
b.call(“a”,2,3); //a

再看一个复杂的例子:

function Animal()
{
this.name=”animal”;
this.showName=function()
{
alert(this.name)
}
}
function Cat()
{
this.name=”cat”;
}
var animal = new Animal();
var cat = new Cat();
animal.showName(); //结果为animal
animal.showName.call(cat); //原本cat没有showName方法,但是通过call方法将animal的showName方法应用到cat上,因此结果为cat

三、实现继承

如下例子:

function Animal(name)
{
this.name=name;
this.showName=function()
{
alert(this.name)
}
}
function Cat(name)
{
Animal.call(this,name); //将Animal应用到Cat上,因此Cat拥有了Animal的所有属性和方法
}
var cat = new Cat(“Black Cat”);
cat.showName(); //浏览器弹出Black Cat

四、apply用法

apply和call的用法只有一个地方不一样,除此之外,其他地方基本一模一样

a.call(b,arg1,arg2…)

apply(b,[arg1,arg2]) //apply只有2个参数,它将call的参数(arg1,arg2…)放在一个数组中作为apply的第二参数

其他总结:

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

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

代码示例:

function Animal(name) {
this.name = name;
this.showName = function() {
console.log(this.name);
};
} function Cat(name) {
Animal.call(this, name);
}
Cat.prototype = new Animal(); //prototype 属性使您有能力向对象添加属性和方法。 function Dog(name) {
Animal.apply(this, name);
}
Dog.prototype = new Animal(); var cat = new Cat("Black Cat"); //call必须是object var dog = new Dog(["Black Dog"]); //apply必须是array cat.showName();
dog.showName(); console.log(cat instanceof Animal);
console.log(dog instanceof Animal);

最新文章

  1. 在 Visual Studio 等编辑器/IDE中自动切换输入法,不需要手动的有没有?
  2. Maven assembly 打包
  3. Ubuntu Dev Box Setup
  4. .NET4.5 WFP中用WebBrowser获取/操作网页html代码
  5. Nginx for Windows 使用笔记
  6. R语言画图实例-参考R语言实战
  7. 【codevs1200】 NOIP2012—同余方程
  8. leetcode 113 Path Sum II ----- java
  9. 简单的flash策略文件服务器!
  10. How to: Extract files from a compiled setup.exe created by Inno setup
  11. [LeetCode 121] - 买入与卖出股票的最佳时机(Best Time to Buy and Sell Stock)
  12. 博客停更及OI退役公告
  13. JavaScript版—贪吃蛇小组件
  14. eclipse下maven插件搭建springmvc之helloworld
  15. 将本地时间转换成 UTC 时间,0时区时间
  16. 【lintcode13】字符串查找
  17. Linux注销&登陆
  18. console call的fallback console 兼容
  19. Matlab中使用LaTeX
  20. StringBuffer 与 StringBuilder类的使用

热门文章

  1. JavaScript练习网站收集
  2. replicated mode vs global mode - 每天5分钟玩转 Docker 容器技术(105)
  3. PF_RING install in centos7
  4. 网页设计——7.css的入门
  5. C# RichTextBox设置行间距
  6. javascript设计模式——中介者模式
  7. ubuntu16.04 Qt5.8 如何使用opecv3.2
  8. 解决failed to push some refs to git
  9. 关于xshell:Connection closed by foreign host
  10. Java内存管理(一)