2017-07-27 59 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我已经更新了我的答案,提供了一次散步。 –