2016-04-22 83 views
1

給定一個正常的功能,如何選擇變量分配給函數的參數? -Javascript

function descriptions(awesome, cool, alright){ 
    return (awesome || "no one") + " is awesome. " + cool + " is cool. " + 
    + alright + " is alright"; 
} 
descriptions("jane", "jack", "jefferson"); 
//returns "jane is awesome. jack is cool. jefferson is alright." 

我想用同樣的功能,但只喜歡傳像這樣的最後兩個參數:

descriptions(cool : "john", alright : "jane"); //I would like a statement similar to this that works. 
//should return "no one is awesome. jack is cool. jefferson is alright." 

如何將上面做什麼?

+1

你不能。 JS不支持命名參數。 – zerkms

+0

我應該刪除我的問題嗎? –

+0

讓我們來看看社區的想法。這是一個有效的問題,「否」是一個完美的答案。 – zerkms

回答

1

你可以通過一個對象做到這一點

var coolLevels = { 
    isCool: ["Jack", "John"] 
, isAlright: ["Jane", "Jefferson"] 
, isAwesome: [] 
} 

function describe(people, coolLevel, phrase) { 
    return people.filter(function(person){ 
    return Boolean(coolLevel.indexOf(person)) 
    }).join(", ") + phrase 
} 

function descriptions(people){ 
    var awesome = describe(people, coolLevels.isAwesome, ' is awesome.') 
    var cool = describe(people, coolLevels.isCool, ' is cool.') 
    var alright = describe(people, coolLevels.isCool, ' is alright.') 

    return awesome + cool + alright 
} 

演示:https://jsbin.com/kahawuhelu/edit?js,console,output

+0

爲簡單起見而選擇。 –

1

這在任何ECMAScript(包括JavaScript)中都是不可能的。

這是理論上可以做的事情一樣使用條件,自定義邏輯:

function(a,b,c){ 
    if(arguments.length === 1) { 
     // we're in object mode; 
     b = a.b 
     c = a.c 
     a = a.a || 'default'; 
    } 
} 

但是,這不是建在語言的一部分。

這是可能的,例如:

function foo(a,b,c){return a/(b || 1) + c;} 
foo({c:1,b:2,a:3}) 

還有基於數量的參數有條件地定義值的可能性:

function say (a,b,c) { 
    if(arguments.length === 2) { 
     c = b; 
     b = a; 
     a = 'cat'; 
    } 
    console.log('a ' + a + ' likes a ' + b + ' and a ' + c) 
} 
say('dog', 'bone', 'walk') // a dog likes a bone and a walk 
say('mouse', 'bowl of milk') // a cat likes a mouse and a bowl of milk 
+1

是否有解決方法?也許涉及對象的東西? –

+0

這是否假設a是傳遞給函數的對象? –

+0

第一個示例假定第一個參數('a')是傳遞給函數的對象。但是,該函數必須寫入以預測該參數。 – cwallenpoole

1

是的,你肯定能做到這一點! 如果沒有提供變量,您可以使用許多開發人員用於將變量設置爲默認值的巧妙技巧。

function descriptions(awesome, cool, alright){ 
awesome = awesome || ""; 
if (awesome === "") 
{ 
    return "no one" + " is awesome. " + cool + " is cool. " + 
+ alright + " is alright"; 
} 
else{ 
    return awesome + " is awesome. " + cool + " is cool. " + 
+ alright + " is alright"; 
} 

} 
console.log(descriptions(undefined, "jack", "jefferson")); 

這裏是工作代碼。你也可以傳遞一個空字符串。

3

不同的東西語法,但類似語義可能實現使用對象解構

function descriptions({ awesome = 'no one', cool, alright }) { 
    return awesome + " is awesome. " + cool + " is cool. " + 
    + alright + " is alright"; 
} 

然後,你只是一個對象,具有相應的屬性調用它:

descriptions({ cool: 'a', alright: 'b'}); 
+0

你知道,在過去的兩年中,JS已經遠離了JS,這些新的ES6東西開始看起來像是深度磁貼。 – cwallenpoole

+0

上面的函數是否會傳遞{awesome:「someone」}?將「真棒」設置爲「某人」。 –

+0

@JasonBasanese那其他屬性呢?在目前的實施中,其他人沒有默認值。 – zerkms

1

在ECMAScript中6,這可排序的如果你改變你的參數來接收一個對象並利用destructuring assignment

function descriptions({awesome: awesome = "no one", cool: cool = "", alright: alright = ""} = {}) { 
 
    return awesome + " is awesome. " + 
 
      cool + " is cool. " + 
 
      alright + " is alright"; 
 
} 
 

 

 
var res = descriptions({ cool: "john", alright: "jane" }); 
 

 

 
document.body.textContent = res;

所以我們有一個名爲參數的仿真人。只有調用者需要的東西是花括號。

當然,瀏覽器的支持是有限的,但是轉譯器是可用的。

function descriptions(info) { 
    // Avoid TypeError if no argument is passed 
    if (!info) { 
     info = {}; 
    } 

    return (info.awesome || "no one") + " is awesome. " + (info.cool || "no one") + " is cool. " + (info.alright || "no one") + " is alright."; 
} 

// Use: 
console.log(descriptions({ 
    awesome: "Strong Bad", 
    cool: "The Cheat", 
    alright: "Strong Sad" 
})); 
1

你可以使用不同的方法:

1

你可以通過undefinednull""作爲第一個參數。例如:

descriptions(null, "jack", "jefferson"); 

由於您已經使用awesome || "no one",所以任何falsy值都會足夠好。


另一種方法是改變函數接收的對象:

function descriptions(options) { 
    return (options.awesome || "no one") + " is awesome. " + options.cool + " is cool. " + 
    options.alright + " is alright"; 
} 

descriptions({ cool: "jack", alright: "jefferson" }); 

現在,根據你的瀏覽器支持,可以使用ES6解構參數:

const descriptions = ({ awesome = 'no one', cool, alright }) => (
    `${awesome} is awesome. ${cool} is cool. ${alright} is alright` 
); 

descriptions({ cool: 'jack', alright: 'jefferson' }); 
相關問題