Arguments 对象、call()与apply()
Arguments 对象
arguments:是一个对应于传递给函数的参数的类数组对象。arguments对象是所有(非箭头)函数中都可用的局部变量,你可以使用arguments对象在函数中引用函数的参数。此对象包含传递给函数的每个参数,第一个参数在索引0处。
ps:arguments对象不是一个 Array ,它类似于Array,但除了length属性和索引元素之外没有任何Array属性和方法。但可以被转换为一个真正的Array,方式如下:
// ES5
var args = Array.prototype.slice.call(arguments);
var args = [].slice.call(arguments);
// ES6
const args = Array.from(arguments);
const args = [...arguments];
参考资料:
1、arguments:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/arguments
call()、apply()、bind()——用于改变this指向
call():使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数:接受的是一个参数列表
call巧用:将具有length属性的对象转成数组[].slice.call(arguments),为了提高性能,减少对原型链的追溯,一般使用Array.prototype.slice.call(arguments)
let obj1 = {
a: '我是obj1的a',
fn: function(msg) {
console.log(msg + this.a);
}
}
let obj2 = {
a: '我是obj2的a'
}
// 正常调用
obj1.fn('你说啥?') // 你说啥?我是obj1的a
// call()调用
obj1.fn.call(obj2, '你说啥?') // 你说啥?我是obj2的a
apply():使用一个指定的 this 值和单独给出的一个数组(
['eat', 'bananas'])或数组对象(new Array('eat', 'bananas'))参数来调用一个函数:接受的是一个包含多个参数的数组
const numbers = [5, 6, 2, 3, 7];
const max = Math.max.apply(null, numbers); // apply巧用,将数组转换为参数列表,等同于`Math.max(...numbers)`
console.log(max); // expected output: 7
const min = Math.min.apply(null, numbers);
console.log(min); // expected output: 2
bind():使用一个指定的 this 值和单独给出的一个或多个参数来创建一个函数供后续被调用:接受的是一个参数列表
const module = {
x: 42,
getX: function() {
return this.x;
}
};
const unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// expected output: undefined
const boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// expected output: 42
注意:apply传参是数组,而call、bind是参数列表,且apply和call是一次性传入参数,而bind可以分为多次传入;bind 是返回绑定this之后的函数,便于稍后调用;apply 、call 则是立即执行。
参考资料:
1、call():https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/call
2、apply():https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
3、bind():https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
4、展开语法:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Spread_syntax
Arguments 对象、call()与apply()的更多相关文章
- JavaScript中的apply与call与arguments对象
(一) call方法 语法:presentObj.call(thisObj,arg1,arg2,arg3...) 参数thisObj :将被用作当前对象presentObj的对象. 当thisObj无 ...
- arguments对象、apply()、匿名函数
在学习arguments对象时,碰到的一段code,不是太好理解.原文地址中文(http://www.jb51.net/article/25048.htm).英文(http://www.sitepoi ...
- 永远不要修改arguments对象
案例复现 var obj = { plus: function(arg0, arg1) { return arg0 + arg1; } }; function callMethod(context, ...
- [Effective JavaScript 笔记]第23条:永远不要修改arguments对象
arguments对象并不是标准的Array类型的实例.arguments对象不能直接调用Array方法. arguments对象的救星call方法 使得arguments可以品尝到数组方法的美味,知 ...
- 关于arguments对象以及函数的柯里化;
1.arguments对象 Arguments是个类似数组但不是数组的对象,说他类似数组是因为其具备数组相同的访问性质及方式,能够由arguments[n]来访问对应的单个参数的值,并拥有数组长度属性 ...
- Javascript Arguments,calle,caller,call,apply
一.Arguments 该对象代表正在执行的函数和调用他的函数的参数. [function.]arguments[n] 参数function :选项.当前正在执行的 Function 对象的名字. n ...
- 你不知道的JavaScript--Item11 arguments对象
1.什么是arguments arguments 是是JavaScript里的一个内置对象,它很古怪,也经常被人所忽视,但实际上是很重要的.所有主要的js函数库都利用了arguments对象.所以ag ...
- ES6教程-字符串,函数的参数,了解函数的arguments对象,js面向对象,设计模式-单例模式,解构赋值
前言 主要讲解了ES6对字符串的拓展,包括includes,startsWith和endsWith,另外增加了字符串模板. Start includes()是否包含 startsWith()以什么开头 ...
- JavaScript学习总结(三、函数声明和表达式、this、闭包和引用、arguments对象、函数间传递参数)
一.函数声明和表达式 函数声明: function test() {}; test(); //运行正常 function test() {}; 函数表达式: var test = functio ...
随机推荐
- 在Angular4中使用ng2-baidu-map详解
一.引言 之前在Angular4使用过百度地图,记录一下踩过的坑 二.实现 1.安装 npm install angular2-baidu-map 2.在app.module.ts配置 ak key在 ...
- mycat原理及分表分库入门
1.什么是MyCat: MyCat是一个开源的分布式数据库系统,是一个实现了MySQL协议的服务器,前端用户可以把它看作是一个数据库代理,用MySQL客户端工具和命令行访问,而其后端可以用MySQL原 ...
- centos7,python2和python3共存
安装依赖包 yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk- ...
- 深度解析.NetFrameWork/CLR/C# 以及C#6/C#7新语法
一:什么是.NetFrameWork/ CLR / C# 1:.NetFramework即架构,它是一个语言开发软件,提供了软件开发的框架,使开发更具工程性.简便性和稳定性,这个框架主要是针对于c#语 ...
- 防止ARP欺骗
前言: 曾经因为宿舍里面的同学经常熬夜打游戏,好言相劝不管用,无奈之下使用arp欺骗他们的主机,使之晚上11点之后游戏延迟,掉线,最后,一到11点同学们就都上床睡觉了. 防止arp欺骗的三种思路: 在 ...
- leetcode组合总和 Ⅳ 解题路径
题目: 关于动态规划类题目的思路如何找在上一篇博客 https://www.cnblogs.com/niuyourou/p/11964842.html 讲的非常清楚了,该博客也成为了了leetcode ...
- Xamarin.Forms移动开发系列3:项目剖析
摘要 本文主要进行Xamarin.Forms应用程序剖析. 前言 本文介绍Xamarin.Forms应用程序剖析. 由于本系列重点研究对象为Xamarin.Forms,所以对Xamarin.Andro ...
- MySQL实战45讲学习笔记:第九讲
一.今日内容概要 今天的正文开始前,我要特意感谢一下评论区几位留下高质量留言的同学.用户名是 @某.人 的同学,对文章的知识点做了梳理,然后提了关于事务可见性的问题,就是先启动但是后提交的事务,对数据 ...
- [LeetCode] 711. Number of Distinct Islands II 不同岛屿的个数之二
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [LeetCode] 491. Increasing Subsequences 递增子序列
Given an integer array, your task is to find all the different possible increasing subsequences of t ...