【JavaScript】call和apply区别及使用方法
一、方法的定义
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区别及使用方法的更多相关文章
- JavaScript中的apply()方法和call()方法使用介绍
1.每个函数都包含两个非继承而来的方法:apply()和call(). 2.他们的用途相同,都是在特定的作用域中调用函数. 3.接收参数方面不同,apply()接收两个参数,一个是函数运行的作用域(t ...
- JavaScript中call,apply,bind方法的区别
call,apply,bind方法一般用来指定this的环境. var a = { user:"hahaha", fn:function(){ console.log(this.u ...
- 《JavaScript总结》apply、call和bind方法
在JavaScript中,apply.call.bind这个三个方法,它们的作用都是为了改变某个函数运行时的上下文, 也就是改变函数体内的this指向. 在一个函数里,存在“定义时上下文”.“运行时上 ...
- JavaScript中call,apply,bind方法的总结。
why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user:"追梦子", fn:f ...
- JavaScript中call,apply,bind方法的总结
原文链接:http://www.cnblogs.com/pssp/p/5215621.html why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之 ...
- JavaScript中call,apply,bind方法
why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user:"追梦子", fn:f ...
- javascript篇-----函数apply()和call()
转自:http://www.jb51.net/article/28013.htm 如果没接触过动态语言,以编译型语言的思维方式去理解javaScript将会有种神奇而怪异的感觉,因为意识上往往不可能的 ...
- Javascript call与apply记录
[注]:记录自己对javascript中call与apply的见解 总会有些东西会被人拿出来重复的写来写去,为何? 只是因为自己感觉不够了解,所谓好记性不如烂笔头,并且在写的同时也会或多或少的收获到一 ...
- apply()和call()的方法
apply()和call()的方法的区别 参考文档https://www.cnblogs.com/lengyuehuahun/p/5643625.html 一直都没太明白apply()与call()的 ...
随机推荐
- OSGi 系列(十八)之 基于注解的 Blueprint
OSGi 系列(十八)之 基于注解的 Blueprint 1. 注解实现 blueprint 第一步:bundle 添加 Bundle-Blueprint-Annotation <plugin& ...
- 2013.7.15 非html 标签 ,外层 要用 ‘’
当使用 非 html 标签 是 ,要使用 单引号 作外层 , 双引号 用单层 ,如 <s:if test='direction=="出"'> 可以执行 ,, ...
- 纯css手写圆角气泡对话框 微信小程序和web都适用
嗯……我们设计师强烈要求一定要圆角!圆角的气泡对话框,不要那种尖角的.这其中还遇上了个尴尬的问题,z-index不生效 无非就是两种方法,一种是使用图片再定位拼接起来使用,太简单了具体就不详细的说了. ...
- SPSS-Friedman 秩和检验-非参数检验-K个相关样本检验 案例解析
三人行,必有我师,是不是真有我师?三种不同类型的营销手段,最终的营销效果是否一样,随即区组秩和检验带你进入分析世界 今天跟大家讨论和分享一下:spss-Friedman 秩和检验-非参数检验-K个(多 ...
- MongoDB安装为Windows服务方法与注意事项
MongoDB作为一个基于分布式文件存储的数据库,近两年大受追捧.数据灵活的存取方式和高效的处理使得它广泛用于互联网应用. 最近本人开始在Windows 32位平台下研究MongoDB的使用,为了方便 ...
- spark ml 的例子
一.关于spark ml pipeline与机器学习 一个典型的机器学习构建包含若干个过程 1.源数据ETL 2.数据预处理 3.特征选取 4.模型训练与验证 以上四个步骤可以抽象为一个包括多个步骤的 ...
- Linux内存子系统及常用调优参数
1>内存子系统 1>组件: slab allocator buddy system kswapd pdflush 2>虚拟化环境: PA:进程地址: HA:虚拟机地址: ...
- spring aop方式配置事务中的三个概念 pointcut advice advisor
AOP的3个关键概念 因为AOP的概念难于理解,所以在前面首先对Java动态代理机制进行了一下讲解,从而使读者能够循序渐进地来理解AOP的思想. 学习AOP,关键在于理解AOP的思想,能够使用AOP. ...
- [翻译] FastReport 变量列表使用
使用报表变量时,引用"frxVariables"单元. 变量定义在"TfrxVariable" 类: TfrxVariable = class(TCollect ...
- Delphi XE6 Android拨号函数
http://blog.sina.com.cn/s/blog_44fa172f0101rpex.html Delphi XE6 Android拨号函数 (2014-05-07 17:48:51) 转载 ...