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. Java ORM Hibernate 入门笔记

    一.下载 官网地址:http://hibernate.org/ Hibernate下有ORM(关系型数据库).OGM(NoSQL数据库).Search(对象全文检索).Validator的工具. OR ...

  2. System V IPC 之共享内存

    IPC 是进程间通信(Interprocess Communication)的缩写,通常指允许用户态进程执行系列操作的一组机制: 通过信号量与其他进程进行同步 向其他进程发送消息或者从其他进程接收消息 ...

  3. android中与SQLite数据库相关的类

    为什么要在应用程序中使用数据库?数据库最主要的用途就是作为数据的存储容器,另外,由于可以很方便的将应用程序中的数据结构(比如C语言中的结构体)转化成数据库的表,这样我们就可以通过操作数据库来替代写一堆 ...

  4. SSRS: How to Display Checkbox on Report

    How to Display Checkbox on Report A textbox with Wingdings font type can be used to display a checkb ...

  5. python多进程并发redis

    Redis支持两种持久化方式RDB和AOF,RDB持久化能够快速的储存和回复数据,但在服务器停机时会丢失大量数据,AOF持久化能够高效的提高数据的安全性,但在储存和恢复数据方面要耗费大量的时间,最好的 ...

  6. hibernate框架学习笔记5:缓存

    缓存不止存在与程序中,电脑硬件乃至于生活中都存在缓存 目的:提高效率 比如IO流读写字节,如果没有缓存,读一字节写一字节,效率低下 hibernate中的一级缓存:提高操作数据库的效率 示例: 抽取的 ...

  7. windows下apache报os 10048错误

    在apache的bin目录下运行httpd -k install,报错os10048 (错误信息是跟443端口有关),网上的答案说的是改掉httpd.conf里的默认端口或者关闭占用端口的进程,默认端 ...

  8. x64系统安装ODAC问题经验分享

    64bit系统安装ODAC经验分享 背景: 最近项目里面有用到 WCF+Entity Framework+oracle 这个架构用过的朋友应该都知道,Entity Framework要通过ODAC的方 ...

  9. 第1次作业:小菜鸟的平凡IT梦

    #1.结缘计算机的始末 ##1.1与计算机相识的几年 作为一个95后,出生在一个互联网开始兴盛的时代.我记得小学的时候,开始知道电脑这个东西,学校有了机房,开始有了所谓的电脑课.那时候计算机对于我来说 ...

  10. 咬碎STL空间配置器

    STL空间配置器 一.开场白: 给我的感觉就是,了解是空间配置器的功能,是那么的明了:在看原理,我还是很开心:接下来是360度大转变: 那么长的变量或者函数命名.那么多的宏.不爽,不过,遇上我这种二货 ...