javascript中apply()和call()方法的区别

一、方法的定义 
call方法: 
语法:call(thisObj,Object)
定义:调用一个对象的一个方法,以另一个对象替换当前对象。
说明: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();

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();  //Black Cat
dog.showName();  //Black Dog

console.log(cat instanceof Animal);   //true
console.log(dog instanceof Animal);   //true

ref:http://www.cnblogs.com/qzsonline/archive/2013/03/05/2944367.html

posted @ 2016-08-21 16:49  晴晴加油  阅读(328)  评论(2编辑  收藏  举报