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的更多相关文章

  1. ES6 new syntax of Arrow Function

    Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data ...

  2. ES6中export , export default , import模块系统总结

    最近在学习使用Webpack3的时候发现,它已经可以在不使用babel的情况下使用ES6的模块加载功能了. 说到ES6的模块加载功能,我们先复习一下CommonJS规范吧: 一  . CommonJS ...

  3. ES6学习笔记<四> default、rest、Multi-line Strings

    default 参数默认值 在实际开发 有时需要给一些参数默认值. 在ES6之前一般都这么处理参数默认值 function add(val_1,val_2){ val_1 = val_1 || 10; ...

  4. 探讨ES6的import export default 和CommonJS的require module.exports

    今天来扒一扒在node和ES6中的module,主要是为了区分node和ES6中的不同意义,避免概念上的混淆,同时也分享一下,自己在这个坑里获得的心得. 在ES6之前 模块的概念是在ES6发布之前就出 ...

  5. es6 export与export default 的区别

    相同点: 均可用于导出常量.函数.文件.模块等 不同点: 1.在一个文件中export可以有多个,但export default 只能有一个: export var firstName = 'Mich ...

  6. 解决JS中missing ( before function parameters的错误

    在编写javascript中,常出现在function处提示“missing ( before function parameters”的错误,这是怎么回事? 例如: function String. ...

  7. 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- ...

  8. ES6函数剩余参数(Rest Parameters)

    我们知道JS函数内部有个arguments对象,可以拿到全部实参.现在ES6给我们带来了一个新的对象,可以拿到除开始参数外的参数,即剩余参数(废话好多 O(∩_∩)O~). 这个新的对象和argume ...

  9. ES6 new syntax of Rest and Spread Operators

    Rest and Spread Operators.md Why we need rest and spread operators? var showCollections = function(i ...

随机推荐

  1. [poj2585]Window Pains_拓扑排序

    Window Pains poj-2585 题目大意:给出一个4*4的方格表,由9种数字组成.其中,每一种数字只会出现在特定的位置,后出现的数字会覆盖之前在当前方格表内出现的.询问当前给出的方格表是否 ...

  2. 工作流Activiti5.13学习笔记(一)

    了解工作流 1.工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某 ...

  3. django之urls系统

    Django的urls系统简介 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映 ...

  4. Git 建立仓库及常用命令速查表

    Git新建仓库两种模式: 一.项目在本地时,本地初始化仓库并提交至Coding.Net 新建一个空白目录并进入,执行如下流程 1.git init2.项目代码复制到当前目录3.git add *4.g ...

  5. 20162330 实验一 《Java开发环境的熟悉》 实验报告

    2016-2017-2 实验报告目录: 1 2 3 4 5 20162330 实验一 <Java开发环境的熟悉> 实验报告 课程名称:<程序设计与数据结构> 学生班级:1623 ...

  6. mysql基础篇 - 其他基本操作

    基础篇 - 其他基本操作         其他基本操作 一.实验简介 本节实验中我们将学习并实践数据库的其他基本操作:索引.视图,导入和导出,备份和恢复等. 这些概念对于数据库管理员而言都非常重要,请 ...

  7. python 进程复习

    import os import time ret = os.fork() # 创建子线程 if ret ==0: # 子进程中返回值为0,父进程>0 while True: print('.. ...

  8. 如何书写高效的css样式

    如何书写高效的css样式? 有以下四个关键要素: 1.高效的css 2.可维护的css 3.组件化的css 4.hack-free  css 书写高效的css: 1.使用外联样式替代行间样式或内嵌样式 ...

  9. css3动画transition详解

    一.transition-property 语法: transition-property : none | all | [ <IDENT> ] [ ',' <IDENT> ] ...

  10. vue 内联样式style中的background

    在我们使用vue开发的时候   有很多时候我们需要用到背景图 这个时候会直接使用 内联样式 直接把你拿到的数据拼接上去 注意  在vue中直接使用style时 花括号一定别忘记 还有就是你的url一定 ...