2017-07-27 44 views
-2

在閱讀關於JavaScript的第一類函數在這裏的話,我碰到這個鏈接來: What is meant by 'first class object'? 其中,我發現這個有趣的例子:的Javascript第一類函數

var men = function (objectOfAdmiration) { 
 
    return objectOfAdmiration(); 
 
}; 
 
men.isSweetHeart = true; 
 

 
var women = function (objectOfAdmiration) { 
 
    return objectOfAdmiration(); 
 
}; 
 
women.isSweetHeart = true; 
 

 
var aliens = function (objectOfAdmiration) { 
 
    return objectOfAdmiration(); 
 
}; 
 

 
function like(obj){ 
 
    if (obj.isSweetHeart) { 
 
     return function(){ return "Holy TRUE!"}; 
 
    } 
 
    else { 
 
     return function(){ return "Holy CRAP!"}; 
 
    } 
 
} 
 

 
alert("Men like women is " + men(like(women))); // -> "Holly TRUE!" 
 
alert("Women like men is " + women(like(men))); // -> "Holly TRUE!" 
 

 
alert("Men like aliens is " + men(like(aliens))); // -> "Holly CRAP!" 
 
alert("Aliens like women is " + aliens(like(women))); // -> "Holly TRUE!"

我需要有人解釋這段代碼,就是在這裏執行的事情。特別是表達men(like(women))和/或women(like(men))。這是怎麼制定產生最終結果...

在此先感謝

+0

我知道了PEMDAS規則。我需要知道的是,當'(女性)'表達式返回函數時,會發生什麼情況會發送給函數men()?謝謝 –

回答

2

只要記住你的高中代數「操作順序」:

  • 括號
  • 指數
  • 乘法
  • 部門
  • 加法
  • 減法

括號總是先評估。而且,對於具有嵌套括號的表達式,內部集合在外部集合之前被評估。

所以,用:

men(like(women)) 

like函數首先與women變量傳遞給它的值調用。

無論函數返回什麼,都是傳遞給men函數的參數。就像這樣:

men(result of calling like(women)) 

而且,women(like(men))

是同樣的想法,只是扭轉哪一個首先被調用。


所以,讓我們一步一個腳印吧。

  • 這是非常重要的,你第一次認識到menwomenalien功能其實都是相同的。他們只需調用 即可傳遞給他們,並返回該結果(在這種情況下, 將始終是另一個函數)。你可以推斷這一點,因爲他們所做的一切都是在輸入參數的末尾加上一組括號。 他們每個人,因此期待一個函數被傳遞(因爲 這是你可以調用的唯一的東西)。

因此,讓我們不太關注這些函數,並更多地關注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!"

+0

感謝Scott的回覆,我知道PEMDAS規則。我需要知道的是,當'(女性)'表達式返回函數時,會發生什麼情況會發送給函數men()? –

+0

感謝您對我的支持Scott ...我試圖讓我的頭繞過這整個'返回一個函數'和'調用函數'的東西....讓我工作你解釋什麼,並會回到你的...再次感謝.. –

+0

@ManiMalhotra我已經更新了我的答案,提供了一次散步。 –

相關問題