[js高手之路] es6系列教程 - 函数的默认参数详解
在ES6之前,我们一般用短路表达式处理默认参数
function show( a, b ){
var a = a || 10;
var b = b || 20;
console.log( a, b );
}
show( 100, 200 ); //100, 200
show(); //10, 20
show( 0, 0 ); // 10, 20, 0会被当做false
短路表达式(就是上例中的 || )的运算规则是:
var res = a || 20; 如果a是true 就返回a, 如果a是false就返回20;
上述例子中, 第八行代码,本意是输出0, 0, 结果0被当做false, 在传递参数0的时候,输出了后面的默认值。为了严谨,我们可以用undefined判断,如下:
function show( a, b ){
var a = typeof a !== 'undefined' ? a : 10;
var b = typeof b !== 'undefined' ? b : 20;
console.log( a, b );
}
show( 100, 200 ); //100,200
show( 0, 0 ); //0,0
上述例子默认参数是以前的做法,而es6,提供了类似php的函数默认参数语法.
function show( name = 'ghostwu', age = 22, sex = 'man' ){
console.log( name, age, sex );
}
show(); //ghostwu, 22, man 使用name, age, sex的默认参数
show( 'zhangsan' );//zhangsan, 22, man 使用age和sex的默认参数
show( 'zhangsan', 30 );//zhangsan, 30, man 使用sex的默认参数
show( 'zhangsan', 40, '男' ); //zhangsan, 40, 男 不使用默认参数
默认参数就是在形式参数后面给他赋一个默认的值.
function show( name, age = 22, sex ){
console.log( name, age, sex );
}
//函数在没有传值得时候,默认为undefined
show();//undefined,22,undefined 使用name,age,sex的默认参数
//函数显示的传递undefined,相当于没有传递参数,所以age用默认值22
show( undefined, undefined, undefined ); //undefined,22,undefined
//函数传递null的时候,不会等于undefined,相当于传null值, 会把age的默认值覆盖
show( undefined, null, undefined ); //undefined, null, undefined
默认参数对arguments会用影响吗?
function show( name, age ){
console.log( name == arguments[0] ); //true
console.log( age == arguments[1] ); //true
name = 'zhangsan';
age = 30;
console.log( name == arguments[0] ); //true
console.log( age == arguments[1] ); //true
}
show( 'ghostwu', 22 );
在es5的非严格模式下,参数的值如果在函数中被修改了,同样会影响(同步)到arguments对象,所以上述结果都是true.
严格模式就是在js代码的最上方加上" use strict", 非严格模式自然就是没有这行代码
如果使用严格模式,参数的值修改之后,就不会影响(同步)到arguments对象
"use strict";
function show( name, age ){
console.log( name == arguments[0] ); //true
console.log( age == arguments[1] ); //true
name = 'zhangsan';
age = 30;
console.log( name, age, arguments[0], arguments[1] ); //zhangsan, 30, ghostwu, 22
console.log( name == arguments[0] ); //false
console.log( age == arguments[1] ); //false
}
show( 'ghostwu', 22 );
而在采用了默认参数之后,在es6中,不管是否启用严格模式,arguments对象一直保存的是函数调用时候传递的参数
function show( name, age = 22 ){
console.log( arguments.length ); //
console.log( name === arguments[0] ); //true
console.log( age, arguments[1] ); //22, undefined
console.log( age === arguments[1] ); //false
name = 'zhangsan';
age = 30;
console.log( name, age, arguments[0], arguments[1] );//zhangsan, 30, ghostwu, undefined
console.log( name == arguments[0] ); //false
console.log( age == arguments[1] ); //false
}
show( 'ghostwu' );
默认参数,还可以使用表达式; 允许把前面的参数值赋值给后面的参数, 但是不能把后面的参数赋值给前面的参数
function getVal(){
return 10;
}
function add( a, b = getVal() ){
return a + b;
}
//相当于 a = 100, b = 200
console.log( add( 100, 200 ) ); //
//相当于 a = 100, b没有传值, 采用getVal()的返回值10
console.log( add( 100 ) ); //
let count = 10;
function getVal(){
return count++;
}
function add( a, b = getVal() ){
return a + b;
}
console.log( add( 100, 200 ) ); //
//count是全局变量,下面这行代码执行完后 count = 11
console.log( add( 100 ) ); //
//相当于a = 100, b = 11
console.log( add( 100 ) ); //
function add( a, b = a){
return a + b;
}
//a = 10 b = 10(a的值)
console.log( add( 10 ) ); //
//a = 10 b = 10(传给b的值)
console.log( add( 10, 10 ) ); //
function getVal( val ){
return val + 10;
}
function add( a, b = getVal( a ) ){
return a + b;
}
//a = 10, b = 20 getVal()的返回值
console.log( add( 10 ) ); //
//a = 10, b = 30
console.log( add( 10, 30 ) ); //
function add( a = b, b ){
return a + b;
}
// a = 10 b = 20
console.log( add( 10, 20 ) ); //
//不能把后面的参数赋给前面的参数
console.log( add( undefined, 20 ) ); //报错
[js高手之路] es6系列教程 - 函数的默认参数详解的更多相关文章
- [js高手之路] es6系列教程 - 迭代器,生成器,for...of,entries,values,keys等详解
接着上文[js高手之路] es6系列教程 - 迭代器与生成器详解继续. 在es6中引入了一个新的循环结构for ....of, 主要是用来循环可迭代的对象,那么什么是可迭代的对象呢? 可迭代的对象一般 ...
- [js高手之路] es6系列教程 - 对象功能扩展详解
第一:字面量对象的方法,支持缩写形式 //es6之前,这么写 var User = { name : 'ghostwu', showName : function(){ return this.nam ...
- [js高手之路] es6系列教程 - 迭代器与生成器详解
什么是迭代器? 迭代器是一种特殊对象,这种对象具有以下特点: 1,所有对象都有一个next方法 2,每次调用next方法,都会返回一个对象,该对象包含两个属性,一个是value, 表示下一个将要返回的 ...
- [js高手之路] es6系列教程 - promise常见用法详解(resolve,reject,catch,then,all,race)
关于promise我在之前的文章已经应用过好几次,如[js高手之路]Node.js+jade+express+mongodb+mongoose+promise实现todolist,本文就来讲解下pro ...
- [js高手之路]es6系列教程 - 解构详解
解构通俗点说,就是通过一种特定格式,快捷的读取对象/数组中的数据的方法, es6之前,我们通过对象名称[键] 读取数据 var User = { 'name' : 'ghostwu', 'age' : ...
- [js高手之路] es6系列教程 - 箭头函数详解
箭头函数是es6新增的非常有意思的特性,初次写起来,可能会觉得别扭,习惯之后,会发现很精简. 什么是箭头函数? 箭头函数是一种使用箭头( => )定义函数的新语法, 主要有以下特性: 不能通过n ...
- [js高手之路] es6系列教程 - var, let, const详解
function show( flag ){ console.log( a ); if( flag ){ var a = 'ghostwu'; return a; } else { console.l ...
- [js高手之路] es6系列教程 - 不定参数与展开运算符(...)
三个点(...)在es6中,有两个含义: 用在形参中, 表示传递给他的参数集合, 类似于arguments, 叫不定参数. 语法格式: 在形参面前加三个点( ... ) 用在数组前面,可以把数组的值 ...
- [js高手之路] es6系列教程 - Set详解与抽奖程序应用实战
我们还是从一些现有的需求和问题出发,为什么会有set,他的存在是为了解决什么问题? 我们看一个这样的例子,为一个对象添加键值对 var obj = Object.create( null ); obj ...
随机推荐
- 关于php的一些安全知识
绝不要以明文形式显示或发送密码.即使是对密码的所有者也应该这样.如果你需要 "忘记密码" 的功能,可以随机生成一个新的 一次性的(这点很重要)密码,然后把这个密码发送给用户 你希望 ...
- 淘宝tairKV分布式
Tair是什么 Tair是由淘宝开发的key/value方案,系统默认支持基于内存和文件的存储引擎,对应于通常我们所说的缓存和持久化存储,这里可以获取更多关于tair的信息,淘宝团队介绍,Tair在淘 ...
- AngularJS事件
<body ng-app="myApp"> <div ng-controller="myCtrl"> <button ng-cli ...
- MacTex XeLaTex xdvipdfmx:fatal: pdf_ref_obj(): passed invalid object. 报错的解决方法
在使用MacTex配合TexStudio编译beamer的时候,爆出如下错误, xdvipdfmx:fatal: pdf_ref_obj(): passed invalid object. 结果尝试其 ...
- Vijos 1007 绕钉子的长绳子
背景 平面上有N个圆柱形的大钉子,半径都为R,所有钉子组成一个凸多边形. 现在你要用一条绳子把这些钉子围起来,绳子直径忽略不计. 描述 求出绳子的长度 格式 输入格式 第1行两个数:整数N(1< ...
- usaco training 4.1.3 fence6 题解
Fence Loops题解 The fences that surround Farmer Brown's collection of pastures have gotten out of cont ...
- Java 容器在实际项目中的应用
前言:在java开发中我们离不开集合数组等,在java中有个专有名词:"容器" ,下面会结合Thinking in Java的知识和实际开发中业务场景讲述一下容器在Web项目中的用 ...
- 针对双系统ubuntu16.04卡死及系统没有声音解决方法
楼主电脑系统状况:win10主系统,128固态为ubuntu系统 安装一共为两次. 第一次出现ubuntu安装成功后没有声音,主系统win10有声音,Ubuntu上检测不到声卡,说明ubu ...
- Unreal Engine 4 Radiant UI 入门教程(零)在场景中摆放网页
相关的学习资源: https://forums.unrealengine.com/showthread.php?12097-PLUGIN-RadiantUI-SDK-UIs-HUDs-Interact ...
- 详解Java API之正则表达式
正则表达式描述的是一种规则,符合这种限定规则的字符串我们认为它某种满足条件的,是我们所需的.在正则表达式中,主要有两种字符,一种描述的是普通的字符,另一种描述的是元字符.其中元字符是整个正则表达式的核 ...