りょういっとん

https://ithelp.ithome.com.tw/articles/10219603
例1:
function letAdamLogin() {
let array = [];
for (let i = 0; i < 1000000; i++) {
array.push(i);
}
return console.log("Access Granted to Adam");
}
function letEvalLogin() {
let array = [];
for (let i = 0; i < 1000000; i++) {
array.push(i);
}
return console.log("Access Granted to Adam");
}
letAdamLogin();
letEvalLogin();
const getAccessTo = (name) => "Access Granted to " + name;
function letUserLogin(user) {
let array = [];
for (let i = 0; i < 1000000; i++) {
array.push(i);
}
return console.log(getAccessTo(user));
}
function letAdminLogin(user) {
let array = [];
for (let i = 0; i < 1000000; i++) {
array.push(i);
}
return console.log(getAccessTo(user));
}
letUserLogin("Andy");
letAdminLogin("peter")
const getAccessTo = (name) => console.log("Access Granted to " + name);
//認証fn
function authenticate(verify) {
let array = [];
for (let i = 0; i < verify; i++) {
array.push(i);
}
return true; //全て認証した
}
//全体のfn (Higher Order Functions)
function letPerson(person, fn) {
if (person.level === "admin") {
return authenticate(50000);
} else if (person.level === "user") {
return authenticate(10000);
}
return getAccessTo(person.name);
}
letPerson({ level: "admin", name: "Andy" }, authenticate);例2
const multiplyBy = function (num1) {
return function (num2) {
return num1 * num2;
};
};
or
const multiplyBy = (num1) => (num2) => num1 * num2;
//可重複使用
const multiplyByTwo = multiplyBy(2);
console.log(multiplyByTwo(3));
console.log(multiplyByTwo(5));
const multiplyByTen = multiplyBy(10);
console.log(multiplyByTen(4));例3
function heavyDuty(index) {
const bigArray = new Array(5000).fill("wow");
return bigArray[index];
}
heavyDuty(399);
//每次都整bigArray
//做只run1次既bigArray fn
function heavyDuty(index) {
const bigArray = new Array(5000).fill("wow");
console.log("made a bigArray");
return (index) => bigArray[index];
}
const findData = heavyDuty();
console.log(findData(388));
console.log(findData(388));
console.log(findData(388));
addA 関数が 2 番目の関数を返すと、addA の実行環境が終了し、関数 numA をとる方法がないのですが、 関数addBでは、それへの参照があります。ですから、彼は外部環境 (Scpoe チェーン) に目を向け、JS エンジンはこの目的のためにこの関数のメモリ領域を予約し、解放しません。
りょういっとん
香港出身のリョウです。旅行とアニメが好きです。僕の夢は世界中のアイスを全部食べてみたい。
トップページへ戻る