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 ...
随机推荐
- Repeating Decimals UVA - 202
The / repeats indefinitely with no intervening digits. In fact, the decimal expansion of every ratio ...
- 系统右键添加cmd命令
我们运行cmd.exe时,会发现刚刚打开时,一般提示在默认路径下: 有时候我们希望直接能够切换到某个路径下运行程序或者做某些工作,那么切换路径可能就会比较麻烦.下面我们介绍一种比较实用的方法,用鼠标右 ...
- 第一次作业:我与CS的缘分
"既然选择了远方,便只顾风雨兼程" --汪国真 一.结缘计算机 1.1初识计算机 当第一次看到这个作业题目的时候,我的思虑不禁回到了小时候那个对这个世界的一切充满兴趣的纯真年代 ...
- Beta冲刺置顶随笔
项目名称:城市安全风险管控系统 小组成员: 张梨贤.林静.周静平.黄腾飞 Beta冲刺随笔 Beta预备 Beta冲刺Day1 Beta冲刺Day2 Beta冲刺Day3 Beta冲刺Day4 Bet ...
- Python实现网站模拟登陆
一.实验简介 1.1 基本介绍 本实验中我们将通过分析登陆流程并使用 Python 实现模拟登陆到一个实验提供的网站,在实验过程中将学习并实践 Python 的网络编程,Python 实现模拟登陆的方 ...
- Flask 学习 十 博客文章
提交和显示博客文章 app/models.py 文章模型 class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer ...
- L2 约束的最小二乘学习法
\[ \begin{align*} &J_{LS}{(\theta)} = \frac { 1 }{ 2 } { \left\| \Phi \theta - y \right\| }^{ 2 ...
- github提交代码到服务器的方法
第一种情况,没有冲突:1.git add .//进入到center的项目下将本地文件打包的意思2.git pull origin dev//将服务器的代码下载到本地如果是最新的会提示Already u ...
- Python-模块使用-Day6
Python 之路 Day6 - 常用模块学习 本节大纲: 模块介绍time &datetime模块randomossysshutiljson & picleshelvexml处理ya ...
- Python3 re模块(正则表达式)
一:什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. (在Python中)它内嵌在Python中,并通过r ...