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

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

二、两者区别
两个方法基本区别在于传参不同
2.1、call方法:

function Product(name, price) {
this.name = name;
this.price = price;
if(price < 0)
throw RangeError('Cannot create product "' + name + '" with a negative price');
return this;
} function Food(name, price) {
Product.call(this, name, price);
this.category = 'food';
}
Food.prototype = new Product(); function Toy(name, price) {
Product.call(this, name, price);
this.category = 'toy';
}
Toy.prototype = new Product();
var cheese = new Food('feta', 5);
var fun = new Toy('robot', 40);

2.2、apply方法:

function Product(name, price) {
this.name = name;
this.price = price;
if(price < 0)
throw RangeError('Cannot create product "' + name + '" with a negative price');
return this;
} function Food(name, price) {
Product.apply(this, arguments);
this.category = 'food';
}
Food.prototype = new Product(); function Toy(name, price) {
Product.apply(this, arguments);
this.category = 'toy';
}
Toy.prototype = new Product();
var cheese = new Food('feta', 5);
var fun = new Toy('robot', 40);

三、作用实例

3.1、类的继承

function Person(name, age) {
this.name = name;
this.age = age;
this.alertName = function() {
alert(this.name);
}
this.alertAge = function() {
alert(this.age);
}
} function webDever(name, age, sex) {
Person.call(this, name, age);
this.sex = sex;
this.alertSex = function() {
alert(this.sex);
}
}
var test = new webDever(“设计蜂巢”, 24, ”男”);
test.alertName(); //设计蜂巢
test.alertAge(); //
test.alertSex(); //男

3.2、回调函数

function Album(id, title, owner_id) {
this.id = id;
this.name = title;
this.owner_id = owner_id;
};
Album.prototype.get_owner = function(callback) {
var self = this;
$.get('/owners/' + this.owner_id, function(data) {
callback && callback.call(self, data.name);
});
};
var album = new Album(1, '设计蜂巢 ', 2);
album.get_owner(function(owner) {
alert('The album ' + this.name + 'belongs to ' + owner);
});

【JavaScript】call和apply区别及使用方法的更多相关文章

  1. JavaScript中的apply()方法和call()方法使用介绍

    1.每个函数都包含两个非继承而来的方法:apply()和call(). 2.他们的用途相同,都是在特定的作用域中调用函数. 3.接收参数方面不同,apply()接收两个参数,一个是函数运行的作用域(t ...

  2. JavaScript中call,apply,bind方法的区别

    call,apply,bind方法一般用来指定this的环境. var a = { user:"hahaha", fn:function(){ console.log(this.u ...

  3. 《JavaScript总结》apply、call和bind方法

    在JavaScript中,apply.call.bind这个三个方法,它们的作用都是为了改变某个函数运行时的上下文, 也就是改变函数体内的this指向. 在一个函数里,存在“定义时上下文”.“运行时上 ...

  4. JavaScript中call,apply,bind方法的总结。

    why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user:"追梦子", fn:f ...

  5. JavaScript中call,apply,bind方法的总结

    原文链接:http://www.cnblogs.com/pssp/p/5215621.html why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之 ...

  6. JavaScript中call,apply,bind方法

    why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user:"追梦子", fn:f ...

  7. javascript篇-----函数apply()和call()

    转自:http://www.jb51.net/article/28013.htm 如果没接触过动态语言,以编译型语言的思维方式去理解javaScript将会有种神奇而怪异的感觉,因为意识上往往不可能的 ...

  8. Javascript call与apply记录

    [注]:记录自己对javascript中call与apply的见解 总会有些东西会被人拿出来重复的写来写去,为何? 只是因为自己感觉不够了解,所谓好记性不如烂笔头,并且在写的同时也会或多或少的收获到一 ...

  9. apply()和call()的方法

    apply()和call()的方法的区别 参考文档https://www.cnblogs.com/lengyuehuahun/p/5643625.html 一直都没太明白apply()与call()的 ...

随机推荐

  1. NOIP 2016 游记

  2. 孤立森林(isolation forest)

    1.简介 孤立森林(Isolation Forest)是另外一种高效的异常检测算法,它和随机森林类似,但每次选择划分属性和划分点(值)时都是随机的,而不是根据信息增益或者基尼指数来选择. 在建树过程中 ...

  3. Caused by: java.lang.NoClassDefFoundError at com.jc.zm.ZmAlarmAction.analyDo(ZmAlarmAction.java:198)

    ZmPlanDayAction objPlanDay = new ZmPlanDayAction();   objPlanDay.setDao(dao);   objPlanDay.setServle ...

  4. Unicode、UTF-8 和 ISO8859-1

    Unicode.UTF-8 和 ISO8859-1到底有什么区别 1.本文主要包括以下几个方面:编码基本知识,java,系统软件,url,工具软件等. 在下面的描述中,将以"中文" ...

  5. PTA第一次作业和第二次作业

    PTA的第一次作业第一题: #include <stdio.h> int main (void) { int grade,i,N ,a=0,b=0,c=0,d=0,e=0; printf( ...

  6. hdu-1087(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087 思路:每确定一个数,后面一个数肯定比它大.所以可以先从最后一个数开始,不断向前确定前面的状态,推 ...

  7. 命令行中开启mySQL数据库服务

    sudo /Applications/XAMPP/xamppfiles/bin/mysql.server start  

  8. (并查集 贪心思想)Supermarket -- POJ --1456

    链接: http://poj.org/problem?id=1456 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  9. HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏

    Sum It Up Problem Description Given a specified total t and a list of n integers, find all distinct ...

  10. Bellman_ford货币兑换——正权回路判断

    POJ1860 题目大意:你在某一点有一些钱,给定你两点之间钱得兑换规则,问你有没有办法使你手里的钱增多.就是想看看转一圈我的钱能不能增多,出现这一点得条件就是有兑换钱得正权回路,所以选择用bellm ...