只要記住你的高中代數「操作順序」:
括號總是先評估。而且,對於具有嵌套括號的表達式,內部集合在外部集合之前被評估。
所以,用:
men(like(women))
的like
函數首先與women
變量傳遞給它的值調用。
無論函數返回什麼,都是傳遞給men
函數的參數。就像這樣:
men(result of calling like(women))
而且,women(like(men))
是同樣的想法,只是扭轉哪一個首先被調用。
所以,讓我們一步一個腳印吧。
- 這是非常重要的,你第一次認識到
men
,women
和alien
功能其實都是相同的。他們只需調用 即可傳遞給他們,並返回該結果(在這種情況下, 將始終是另一個函數)。你可以推斷這一點,因爲他們所做的一切都是在輸入參數的末尾加上一組括號。 他們每個人,因此期待一個函數被傳遞(因爲 這是你可以調用的唯一的東西)。
因此,讓我們不太關注這些函數,並更多地關注like
函數,因爲這是將函數返回給其調用者的函數。
讀取內嵌批註的說明:
// Function expression that sets men to hold a function
var men = function (objectOfAdmiration) {
// This function is expecting a function to be passed to it
// because all it does is invoke what is passed and only
// functions can be invoked.
return objectOfAdmiration();
};
// Treat men like an object now and give it an isSweetHeart property with a value of true
men.isSweetHeart = true;
// Function expression that sets women to hold a function
var women = function (objectOfAdmiration) {
// This function is expecting a function to be passed to it
// because all it does is invoke what is passed and only
// functions can be invoked.
return objectOfAdmiration();
};
// Treat men like an object now and give it an isSweetHeart property with a value of true
women.isSweetHeart = true;
// Function expression that sets alients to hold a function
var aliens = function (objectOfAdmiration) {
// This function is expecting a function to be passed to it
// because all it does is invoke what is passed and only
// functions can be invoked.
return objectOfAdmiration();
};
// Function declaration for like.
// This function is expecting an object that has an isSweetHeart property.
// Since we have multiple types of objects that support that property, this
// function is polymorphic
function like(obj){
if (obj.isSweetHeart) {
// If the men or women objects are passed, this will be returned
return function(){ return "Holy TRUE!"};
}
else {
// Anything that doesn't have the isSweetHeart property or does
// but that property has a "falsey" value will end up here
return function(){ return "Holy CRAP!"};
}
}
// Invoke like(woman)...
// since the woman object does have an isSweetHeart property and that
// property has a value of true, the function that will return "Holy TRUE!"
// when it is invoked is returned.
// That function is then passed to the men function, which recieves it under
// the parameter name of "objectOfAdmiration".
// Invoking that function outputs "Holy TRUE!"
alert("Men like women is " + men(like(women))); // -> "Holy TRUE!"
// This line does just about the same as above, but in reverse and
// because the men and women functions are the same, the same output
// is produced.
alert("Women like men is " + women(like(men))); // -> "Holy TRUE!"
// Same general procedure as before, but since aliens do not even
// have an isSweetHeart property, a different function is returned
// for further processing.
alert("Men like aliens is " + men(like(aliens))); // -> "Holy CRAP!"
// This is essentially the same as the men(like(women)) function call
// except that the result of like(women) is passed to aliens, which
// Simply invokes that function.
alert("Aliens like women is " + aliens(like(women))); // -> "Holy TRUE!"
我知道了PEMDAS規則。我需要知道的是,當'(女性)'表達式返回函數時,會發生什麼情況會發送給函數men()?謝謝 –