所以就像我需要在速記或函式中撰寫 if/else 陳述句而不是:
if (hero === "Robin"){return callRobin()}... 或代替 switch case。
const callRobin = () => `Hey Robin`;
const callRaven = () => `Hey Raven`;
const callStarFire = () => `Hey StarFire`;
const callBeastBoy = () => `Hey BeastBoy`;
// these were the functions!!
const herosFuncArr = [callRobin, callRaven, callStarFire, callBeastBoy]; //an array that contains the functions
const herosStringsArr = ['Robin', 'Raven', 'StarFire', 'BeastBoy'];
const myFunc = param => param == herosStringsArr.filter(x => x.includes(param)) ? herosFuncArr.filter(z => z.name.includes(param)()) : false;
myFunc('StarFire');
我在這段代碼中的觀點是:當我們輸入一個英雄名字作為引數時,如果它存在于字串陣列中,則從函式陣列中回傳一個元素,該元素具有與引數相同的字母作為函式,如雙括號所示。
我嘗試了很多東西,也嘗試過 eval (`call${param}()) 但顯然這是不可接受的。也試過 .toString 但沒有用(對我來說)。任何幫助,將不勝感激。
uj5u.com熱心網友回復:
您最好消除所有這些函式,并創建一個callHero函式,您可以將找到的英雄的名稱傳遞給該函式并回傳一個字串。您無需擔心filter;用于find查找第一個匹配項。
而且它最好不要使用includes,因為這將匹配Star到StarFire你可能不希望這樣做。只是做一個簡單的比較。
const heroes = ['Robin', 'Raven', 'StarFire', 'BeastBoy'];
// Return a string
function callHero(hero) {
return `Hey ${hero}!`;
}
function isAHero(name) {
// Find the hero in the array
const hero = heroes.find(hero => hero === name);
// If it exists call the `callHero` function with the hero name
// and return the resulting string from the function
if (hero) return callHero(hero);
// Otherwise return something else
return `Boo! ${name} is not a hero.`;
}
console.log(isAHero('Robin'));
console.log(isAHero('Billy Joel'));
console.log(isAHero('StarFire'));
console.log(isAHero('Star'));
uj5u.com熱心網友回復:
這可能對你有用。在 中找到index給定名稱的herosStringsArr,然后在herosFuncArrat 中獲取函式index
const callRobin = () => `Hey Robin`;
const callRaven = () => `Hey Raven`;
const callStarFire = () => `Hey StarFire`;
const callBeastBoy = () => `Hey BeastBoy`;
// these were the functions!!
const herosFuncArr = [callRobin, callRaven, callStarFire, callBeastBoy]; //an array that contains the functions
const herosStringsArr = ['Robin', 'Raven', 'StarFire', 'BeastBoy'];
const myFunc = param => {
const index = herosStringsArr.findIndex(el => el === param);
if (index >= 0) {
return herosFuncArr[index];
} else {
return undefined;
}
};
const func = myFunc('StarFire');
if (typeof func === 'function') {
console.log(func.toString());
console.log(func.call());
}
uj5u.com熱心網友回復:
您可以呼叫函式并比較回傳的字串:
const callRobin = () => `Hey Robin`;
const callRaven = () => `Hey Raven`;
const callStarFire = () => `Hey StarFire`;
const callBeastBoy = () => `Hey BeastBoy`;
// these were the functions!!
const herosFuncArr = [callRobin, callRaven, callStarFire, callBeastBoy]; //an array that contains the functions
const herosStringsArr = ['Robin', 'Raven', 'StarFire', 'BeastBoy'];
const myFunc = param => param == herosStringsArr.filter(x => x.includes(param)) ? herosFuncArr.filter(z => z().includes(param)) : false;
console.log(myFunc('StarFire'));
uj5u.com熱心網友回復:
您可以在 Map 的幫助下完成。你應該把 heroStringsArr 作為鍵,你的函式作為值:
const map = new Map();
map.set('Robin', () => console.log('Hey Robin'));
map.set('Raven', () => console.log('Hey Raven'));
map.set('StarFire', () => console.log('Hey StarFire'));
map.set('BeastBoy', () => console.log('Hey BeastBoy'));
const myFunc = param => map.has(param) ? map.get(param) : console.log('This function is not available!');
myFunc('Robin')();
uj5u.com熱心網友回復:
我喜歡@Andy 的只有一個函式的想法。此外,如果您必須有許多函式,本質上是同一函式的多個“實體”,您可以從一個字串陣列開始,然后您可以使用一個函式將其映射到一個函式陣列。
您也可以像下面的演示一樣使用正則運算式:
const callRobin = () => `Hey Robin`;
const callRaven = () => `Hey Raven`;
const callStarFire = () => `Hey StarFire`;
const callBeastBoy = () => `Hey BeastBoy`;
// these were the functions!!
const herosFuncArr = [callRobin, callRaven, callStarFire, callBeastBoy]; //an array that contains the functions
const herosStringsArr = ['Robin', 'Raven', 'StarFire', 'BeastBoy'];
const myFunc = param => herosFuncArr.filter(f => (new RegExp(`\\b${param}\\b`)).test( f() )).length > 0;
console.log( myFunc('StarFire') );
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/338407.html
上一篇:如何在tkinter中將碰撞添加到物件(圓圈)串列中?
下一篇:JS函式變數看不到全域變數
