arguments的介绍(一)
arguments 是一个类数组对象。代表传给一个function的参数列表。
1.1 arguments length
arguments 是个类数组对象,其包含一个 length
属性,可以用 arguments.length
来获得传入函数的参数个数。
function func() {
console.log("The number of parameters is " + arguments.length);
} func();
func(1, 2);
func(1, 2, 3);
执行结果如下:
The number of parameters is 0
The number of parameters is 2
The number of parameters is 3
1.2 arguments 转数组
通常使用下面的方法来将 arguments 转换成数组:
Array.prototype.slice.call(arguments);
[].slice.call(arguments);
1.3 leaking arguments
另外,有一个需要注意的地方就是,不能将函数的 arguments 泄露或者传递出去。什么意思呢?看下面的几个泄露 arguments 的例子:
// Leaking arguments example1:
function getArgs() {
return arguments;
} // Leaking arguments example2:
function getArgs() {
const args = [].slice.call(arguments);
return args;
} // Leaking arguments example3:
function getArgs() {
const args = arguments;
return function() {
return args;
};
}
上面的做法就直接将函数的 arguments 对象泄露出去了,最终的结果就是 V8 引擎将会跳过优化,导致相当大的性能损失。
可以这样做:
function getArgs3 () {
const args = new Array(arguments.length)
for (let i = 0; i < args.length; i++) {
args[i] = arguments[i]
}
return args;
}
1.4 修改arguments的值
// 修改arguments的值
function foo(a) {
"use strict";
console.log(a, arguments[0]); // 1 1
a = 10;
console.log(a, arguments[0]); // 10 1
arguments[0] = 20;
console.log(a, arguments[0]); // 10 20
}
foo(1);
function bar(a) {
console.log('-----非严格模式下的值变化------')
console.log(a, arguments[0]); // 1 1
a = 10;
console.log(a, arguments[0]); // 10 10
arguments[0] = 20;
console.log(a, arguments[0]); // 20 20
}
bar(1);
从上面的两个例子中可以看出,在严格模式下,函数中的参数与 arguments 对象没有联系,修改一个值不会改变另一个值。而在非严格模式下,两个会互相影响。
arguments的介绍(一)的更多相关文章
- JavaScript函数的柯里化(currying)
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/currying.html 什么是js函数的currying /柯里化? 说到js的柯里化,相信很多朋友都会头大.或 ...
- 前端入门12-JavaScript语法之函数
声明 本系列文章内容全部梳理自以下几个来源: <JavaScript权威指南> MDN web docs Github:smyhvae/web Github:goddyZhao/Trans ...
- 【转】JavaScript里Function函数实现可变参数
转载: http://www.oschina.net/question/54100_15938 使用javascript类库函数时,经常会遇到一个函数,可以使用不同个数的参数的情况 比如:exp(v ...
- 2019前端面试系列——JS高频手写代码题
实现 new 方法 /* * 1.创建一个空对象 * 2.链接到原型 * 3.绑定this值 * 4.返回新对象 */ // 第一种实现 function createNew() { let obj ...
- CodeIgniter安装和入门使用(一)
CodeIgniter是个轻量级功能也强大的框架,适合做自己做小项目用,本文介绍CodeIgniter的安装和使用.安装 官网链接http://codeigniter.org.cn/user_guid ...
- python 格式化输出详解(占位符:%、format、f表达式)——上篇 理论篇
0 - 占位符介绍 要实现字符串的拼接,使用占位符是的一种高效.常用的方式. 举个例子,下面是不使用占位符的一种写法,直接使用加号拼接字符串 name = "Li hua" age ...
- 【转】JavaScript 之arguments、caller 和 callee 介绍
1.前言 arguments, caller , callee 是什么? 在JavaScript 中有什么样的作用?本篇会对于此做一些基本介绍. 本文转载自:http://blog.csdn.ne ...
- python 全栈开发,Day51(常用内置对象,函数,伪数组 arguments,关于DOM的事件操作,DOM介绍)
昨日内容回顾 1.三种引入方式 1.行内js <div onclick = 'add(3,4)'></div> //声明一个函数 function add(a,b){ } 2. ...
- JavaScript 之arguments、caller 和 callee 介绍
1.前言 arguments, caller , callee 是什么? 在javascript 中有什么样的作用?本篇会对于此做一些基本介绍. 2. arguments arguments: ...
随机推荐
- solr添加IK分词和自己定义词库
下载IK分词IK Analyzer 2012FF_hf1.zip 下载地址:http://yunpan.cn/cdvATy8899Lrw (提取码:c10d) 1.将IKAnalyzer2012FF_ ...
- Ubuntu安裝nginx-1.6.2
1.在執行完./configure 和make install 後出現 test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/ ...
- 【POJ】2240 Arbitrage
题目链接:http://poj.org/problem?id=2240 题意:n种国家的货币,m个换算汇率.问你能不能赚钱. eg:1美元换0.5英镑,1英镑换10法郎,1法郎换0.21美元,这样1美 ...
- npm -v 报错:cannot find module 'core-util-is'
今天想打开之前的项目运行看看,结果报错:cannot find module 'core-util-is',以为只是缺少模块core-util-is,然后npm install --save core ...
- 如何使用maven打包
使用maven打包有两种情况:不包括第三方jar包.包括第三方jar包 一般我们写程序,都会在IDEA上去调试,那么也就意味着,你需要将你所需要的jar包(第三方jar)全部给pom文件,否则是不能工 ...
- GetOpenFilename的基本用法
GetOpenFilename '一.概述基本语法 Application.GetOpenFilename 方法 显示标准的“打开”对话框,并获取用户文件名,而不必真正打开任何文件,只是把打开文件名称 ...
- Java 局部变量
Java 局部变量 局部变量声明在方法.构造方法或者语句块中: 局部变量在方法.构造方法.或者语句块被执行的时候创建,当它们执行完成后,变量将会被销毁: 访问修饰符不能用于局部变量: 局部变量只在声明 ...
- Educational Codeforces Round 27 D. Driving Test
单调栈 题意看了半天... #include <cstdio> #include <cstdlib> #include <cmath> #include <c ...
- latex 引用文献 bib
study from : https://jingyan.baidu.com/article/925f8cb8bce1f0c0dce0564f.html 寻找文献 谷歌学术 from: https:/ ...
- leetcode-421-数组中两个数的最大异或值*(前缀树)
题目描述: 方法一: class Solution: def findMaximumXOR(self, nums: List[int]) -> int: root = TreeNode(-1) ...