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. 浅谈new/delete和malloc/free的用法与区别

    每个程序在执行时都会占用一块可用的内存空间,用于存放动态分配的对象,此内存空间称为自由存储区或堆. 一.new和delete用法 如下几行代码: int *pi=new int; int *pi=ne ...

  2. BigDecimal 转成 double

    NUMBER(20,2) 数据库里的字段number  ,实体是BigDecimal 将BigDecimal转成double public double getOrderamount() { if ( ...

  3. Suricata默认规则集相关

    Suricata规则集 Suricata 基于规则检测和控制数据流量,所有规则的配置文件保存在rules目录内 .这些是已知和确认的活动僵尸网络和其C&C(command and contro ...

  4. 基于 HTML5 Canvas 实现的文字动画特效

    前言 文字是网页中最基本的元素,一般我们在网页上都是展示的静态文字,但是就效果来说,还是比较枯燥的.文字淡入淡出的动画效果在项目中非常实用,如果有某些关键的文字,可以通过这种动态的效果来提醒用户阅读. ...

  5. selenium webdriver API

    元素定位 #coding=utf-8 from selenium import webdriver from selenium.webdriver.firefox.firefox_binary imp ...

  6. idea 导eclipse项目

    https://www.cnblogs.com/xiaoBlog2016/archive/2017/05/08/6825014.html

  7. nyoj 韩信点兵

    描述相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排.五人一排.七人一排地变换队形,而他每次只掠一眼队伍的排尾就知道总人数了.输入3个非负整数a,b,c ,表示每种队形排尾的人数 ...

  8. php函数var_dump() 、print_r()、echo()

    var_dump() 能打印出类型 print_r() 只能打出值 echo() 是正常输出... 需要精确调试的时候用 var_dump(); 一般查看的时候用 print_r() 另外 , ech ...

  9. RocketMQ(二):RPC通讯

    匠心零度 转载请注明原创出处,谢谢! RocketMQ网络部署图 NameServer:在系统中是做命名服务,更新和发现 broker服务. Broker-Master:broker 消息主机服务器. ...

  10. python+flask 分分钟完美解析阿里云日志

    拿到了自己阿里云服务器的日志,对其需要进行处理. class Read_Rizhi: def __init__(self,filename): self.filename=filename def o ...