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?

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

ES6 new syntax of Default Function Parameters相关推荐

  1. javacript Function parameters(函数参数)

    Function parameters Default parameters(默认值) function multiply(a, b = 1) {return a * b; }multiply(5); ...

  2. ES6中表达export default const是无效的

    问题 如果您是ES6新手,可以参考一下本文--高手请移驾别往! 请先看下面的图形描述: 也就是说,ES6中default后面是不允许跟const关键字的. 分析 上图中表达可以更换成另一种形式,就可以 ...

  3. 手机拍照打卡活动制作方案,通过拍照不聚集活动,函数参数(Function parameters)是在函数定义中所列的名称。

    <p>本例调用了一个执行计算的函数,然后返回结果:</p><p id="demo"></p><script> funct ...

  4. JQ+ ES6模板字符串 + $.each(数组, function(index, 数组中的对象) {操作程序} 循环遍历添加新html结构标签

    (function() {// 1. 准备相关数据var hotData = [{city: "北京", // 城市sales: "25, 179", // 销 ...

  5. 通过微信小程序来实现线上健步走活动,统计大家的微信运动步数,函数参数(Function parameters)是在函数定义中所列的名称。

    <p id="demo"></p><script> var x = myFunction(7, 8); document.getElementB ...

  6. bar.bind.bind_JavaScript中的function.prototype.bind和function.prototype.length解释

    bar.bind.bind 功能绑定 (Function Bind) bind is a method on the prototype of all functions in JavaScript. ...

  7. 神经网络-感知机(8)[matlab]

    可以使用硬限幅 >> test e = 10000 e = -1     0    -1    -1    -1 Wij = -1.0428   -1.5146 b = -1     0  ...

  8. PHP解析/语法错误; 以及如何解决它们?

    本文翻译自:PHP parse/syntax errors; and how to solve them? Everyone runs into syntax errors. 每个人都遇到语法错误. ...

  9. 【译】2019年开始使用Typescript

    基于2018年Stack Overflow Developer的调研,TypeScript作为编程语言比JavaScript更受"喜爱".TypeScript在js开发者中这么受喜 ...

最新文章

  1. java native方法体在哪_java中native方法的使用
  2. 关于PrintQueueCollection()类,跨线程调用错误“线程无法访问此对象,因为另一个线程拥有该对象”
  3. Ubuntu16.04编译RK3399:make kernel.img error
  4. centos 下安装man手册
  5. 用注解给映射视图的类创建联合主键
  6. 论文阅读:BASNet:Boundary-Aware Salient Object Detection
  7. 红警 for Mac合集(Red Alert红色警戒)
  8. hadoop安装教程(一次填完所有的坑)
  9. 如何在线查看某个地方的高程值
  10. Altera系列板子没有办法sudo,问题解决
  11. Python解微分方程(验证数学建模第五版火箭发射模型)
  12. 电子沙盘 数字沙盘 开发教程
  13. linux脚本对登录密码加密,shell 脚本明文密码加密小工具
  14. java编程符号大全_数学符号大全
  15. python求直角三角形两边求锐角_在二元数组中求直角三角形坐标
  16. 【毕业设计】47-基于单片机的锅炉过热汽温控制系统设计(原理图工程+仿真工程+源代码+答辩论文)
  17. 人人网、7K7K、178等各网站迅雷地址。最新得到的各种URL。
  18. 提示网站服务器403,浏览器打开网页时出现http 403 禁止访问错误是什么原因?
  19. Android Studio汉化(中文支持)
  20. cesium添加自定义点位图片

热门文章

  1. 去中心化钱包CoinU基本介绍,你想知道的都有。
  2. 线程间通讯机制(提高篇)——深入浅出实现原理
  3. Hyperledger Fabric 排序服务核心原理和工作过程
  4. JZOJ 4675. 【NOIP2016提高A组模拟7.21】Double-row
  5. android技巧:apk文件反编译以及签名打包,APKTool 反编译,打包,签名
  6. php 批量删除cookie,php批量删除cookie的简单实现方法
  7. 湘西州计算机等级考试内容,2020年3月湖南省湘西州市计算机等级考试注意事项...
  8. bat启动脚本 springboot_奇葩需求:springboot项目注册为windows系统服务并设置开机自启...
  9. autowired 静态方法使用_关于springboot工具类中@Autowired注入bean,用static直接修饰,静态方法使用bean时报空指针异常错误...
  10. 触摸传感器的电路图符号_光电传感器电路图以及应用和优点