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. 我从业11年来遇到的最奇葩的raid0+1数据恢复经历

    我是一名数据恢复工程师,从事数据恢复行业已经11年了,前几天接到一组4块盘SCSI RAID0+1的数据恢复,客户说做了两组raid1,现在raid状态里显示有3快盘offline.如果两组盘分别作r ...

  2. 10-TypeScript中的接口

    接口是一种规约的约定,从接口继承的类必须实现接口的约定.在高级开发中,通常接口是用于实现各种设计模式的基础,没有接口,设计模式无从谈起. 定义接口: interface ILog{ recordlog ...

  3. 看漫画学Flux

    原文地址:A cartoon guide to Flux - by Lin Clark Flux在目前web开发中最受欢迎也较不被人理解,本文会以简单易懂的方式解释它. 出现问题 首先,我要声明Flu ...

  4. C# HttpClient设置cookies的两种办法 (转发)

    一般有两种办法 第一种handler.UseCookies=true(默认为true),默认的会自己带上cookies,例如 var handler = new HttpClientHandler() ...

  5. 使用 HttpClient 请求 Web Api

    1.获取 post 请求 body 内容 [HttpPost] public string GetId() { //如果方法参数里面有 [FromBody],则需要重新调整内容指针,再进行读取. // ...

  6. node防xss攻击插件

    var xss = require('node-xss').clean; router.post("/orders/insert-orders", function (req, r ...

  7. Oracle10g物理DG详细配置方法及步骤

    --测试环境:    OS:Redhat linux(64)    Primary:    IP:192.168.94.198    SID:dgdb1    Hostname:dg1    DB_U ...

  8. Linux实战案例(7)安装jdk

    一.文件准备 1.1 文件名称 jdk-8u121-linux-x64.tar.gz 1.2 下载地址 http://www.oracle.com/technetwork/java/javase/do ...

  9. Spring Security入门(1-13)Spring Security的投票机制和投票器

    1.三种表决方式,默认是 一票制AffirmativeBased public interface AccessDecisionManager { /** * 通过传递的参数来决定用户是否有访问对应受 ...

  10. MySQL基础操/下

    MySQL基础操 一.自增补充 desc (表名)t1: 查看表格信息内容 表的信息 show create table t1(表名):也是查看信息,还不多是横向查看 show create tabl ...