博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ES6 new syntax of Default Function Parameters
阅读量:5277 次
发布时间:2019-06-14

本文共 2416 字,大约阅读时间需要 8 分钟。

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);

1179506-20180417230014737-308121429.png

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);

1179506-20180417230421793-597331345.png

if this?

function getSum(a, b) {    console.log( a + b);}getSum();getSum(1,2);getSum(10);getSum(null, 6);

1179506-20180417230526307-723339776.png

//ES6function getSum(a = 1, b = 41 ) {    console.log(a + b);}getSum();getSum(1, 2);getSum(10);getSum(null, 6);

1179506-20180417230639247-1862894686.png

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

1179506-20180417231838510-1047411181.png

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();

1179506-20180417231927819-1598926009.png

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());

1179506-20180417232613285-1278504079.png

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?

转载于:https://www.cnblogs.com/InnerPeace-Hecdi/p/8870632.html

你可能感兴趣的文章
DefaultSingletonBeanRegistry源码解析
查看>>
Josephus 问题相关
查看>>
160. Intersection of Two Linked Lists
查看>>
Linux CentOS服务启动
查看>>
01背包
查看>>
1.初次运行git前的配置
查看>>
The prefix "context" for element "context:component-scan" is not bound.
查看>>
STL(一)Containers
查看>>
在JSP中读取POST的JSON数据
查看>>
新的开始
查看>>
Spark1.3.0安装
查看>>
汉罗塔
查看>>
Oracle触发器
查看>>
C#拾遗-边边角角
查看>>
博客园第一天
查看>>
一个老忘且非常有用的jquery动画方法 网页上卷
查看>>
Python 简介和入门
查看>>
ABP 拦截器不工作
查看>>
撩课-Java每天5道面试题第12天
查看>>
UVa 10943 - How do you add?
查看>>