ES6 new syntax of Default Function Parameters
Default Function Parameters.md
Default Function Parameters
function getSum(a,b){
a = (a !== undefined) ? a : 1 ;
b = (b !== undefined) ? b : 41;
console.log(a+b);
}
getSum();
getSum(1,2);
getSum (10);
getSum(null, 6);

In ES5,if the argument is not specified,its value would be set to undefined.
if we do this,what will be happen?
function getSum(a, b) {
a = 1;
b = 41;
console.log(a + b);
}
getSum();
getSum(1,2);
getSum(10);
getSum(null, 6);

if this?
function getSum(a, b) {
console.log( a + b);
}
getSum();
getSum(1,2);
getSum(10);
getSum(null, 6);

//ES6
function getSum(a = 1, b = 41 ) {
console.log(a + b);
}
getSum();
getSum(1, 2);
getSum(10);
getSum(null, 6);

In ES6 sets default values trying to streamline this process.
var getAnswer = function(number = 42, item = "universe"){
console.log(number + " is the answer to " + item);
}
getAnswer(undefined, "life"); //42 is the answer to life.
var getName = function(firstName = "John", lastName = "Doe") {
console.log(firstName + " " + lastName);
}
getName("Jone"); //Jone Doe.
var defaultName = "John";
var getName = function(firstName = defaultName, lastName = "Doe"){
console.log(firstName + " " + lastName);
};
getName(); //John Doe
var getFirstName = () => "John";
var getName = function( firstName = getFirstName(),lastName = "Doe"){
console.log(firstName + " " + lastName);
}
getName(); //John Doe
How to check the numbers of the arguments?
we can check the numbers of the arguments by arguments.length.
var getName = function(firstName, lastName = "Doe"){
console.log(arguments.length);
}
getName("John"); //1

Even thougn the second argument get a default value, arguments.length only returns the number of arguments passed to it.
var getPrice = function(quantity = price, price = 5){
console.log(quantity + " ," + price);
}
getPrice();

This is a TDZ reference Error.
Because the parameters scope just between the parentheses(...).It isn't in a function body scope.
dynamic function
var getNumber = new Function("number = 42", "return number;");
console.log(getNumber());

summary
the type of default arguments
1.set a default value to the parameter in the function declaration statement itself.
2.function default values can be any valid expression.
3.function call
4.We can also access the other variables in the expression used as the default value.
Question what is dynamic function?
ES6 new syntax of Default Function Parameters的更多相关文章
- ES6 new syntax of Arrow Function
Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data ...
- ES6中export , export default , import模块系统总结
最近在学习使用Webpack3的时候发现,它已经可以在不使用babel的情况下使用ES6的模块加载功能了. 说到ES6的模块加载功能,我们先复习一下CommonJS规范吧: 一 . CommonJS ...
- ES6学习笔记<四> default、rest、Multi-line Strings
default 参数默认值 在实际开发 有时需要给一些参数默认值. 在ES6之前一般都这么处理参数默认值 function add(val_1,val_2){ val_1 = val_1 || 10; ...
- 探讨ES6的import export default 和CommonJS的require module.exports
今天来扒一扒在node和ES6中的module,主要是为了区分node和ES6中的不同意义,避免概念上的混淆,同时也分享一下,自己在这个坑里获得的心得. 在ES6之前 模块的概念是在ES6发布之前就出 ...
- es6 export与export default 的区别
相同点: 均可用于导出常量.函数.文件.模块等 不同点: 1.在一个文件中export可以有多个,但export default 只能有一个: export var firstName = 'Mich ...
- 解决JS中missing ( before function parameters的错误
在编写javascript中,常出现在function处提示“missing ( before function parameters”的错误,这是怎么回事? 例如: function String. ...
- ES6 Class vs ES5 constructor function All In One
ES6 Class vs ES5 constructor function All In One ES6 类 vs ES5 构造函数 https://developer.mozilla.org/en- ...
- ES6函数剩余参数(Rest Parameters)
我们知道JS函数内部有个arguments对象,可以拿到全部实参.现在ES6给我们带来了一个新的对象,可以拿到除开始参数外的参数,即剩余参数(废话好多 O(∩_∩)O~). 这个新的对象和argume ...
- ES6 new syntax of Rest and Spread Operators
Rest and Spread Operators.md Why we need rest and spread operators? var showCollections = function(i ...
随机推荐
- c++ --> 返回值分析
返回值分析 函数不能通过返回指向栈内存的指针,返回指向堆内存的指针是可以的. 一.返回局部变量的值 可以有两种情况:返回局部自动变量和局部静态变量,比如: int func() { ; // 返回局部 ...
- linux No space left on device 由索引节点(inode)爆满引发500问题
inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB). 操作系统读取 ...
- iOS之SQLite使用详解
#pragma mark - 1.引入<sqlite3.h>头文件//添加libsqlite3.0.tbd#import <sqlite3.h>static sqlite3 * ...
- JAVA基础之字符串和面向对象
* [String 类] * 1. String类位于java.lang包中,java.lang是java的语言包,使用时无须导包,自动导入. * * 2.拿到一个字符串对象: * ①字面量声明:St ...
- 软工实践项目需求分析(团队)修改版get√-黄紫仪
日常前言:随笔距离文档大体完成已经过去了2天+(因为中间插了一波结对作业),所以目测感受没有那时候清晰(那时候烦的想打人了都--)需求分析那边去百度找了模板.emmmm好多东西感觉听都没听说过QAQ, ...
- Beta Scrum Day 6
听说
- beta冲刺1-咸鱼
前言:这篇算是开始补之前的开端,毕竟beta阶段我们从前面开始就有在陆续做了. 今天的工作: 接收了新成员*1,然后几个人聚了一下,并且讨论了一下目前遇到的问题,以及目前需要处理的问题. 目前遇到的问 ...
- 每日冲刺报告——Day2(Java-Team)
第二天报告(11.3 周五) 团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://git ...
- 一个页面多个HTTP请求 页面卡顿!
用promise解决 前两天面试的时候 一个面试官问到这样一个问题 这里先说出解决的路径 这几天会更新具体的做法 或者直接参考廖雪峰大神 地址如下: https://www.liaoxuefeng.c ...
- Python内置函数(51)——hasattr
英文文档: hasattr(object, name) The arguments are an object and a string. The result is True if the stri ...