日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ES6 new syntax of Default Function Parameters

發布時間:2025/3/14 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ES6 new syntax of Default Function Parameters 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。