Javascript头等功能

时间:2017-07-27 19:42:16

标签: javascript first-class-functions

在SO上阅读有关Javascript First Class Functions的内容时,我遇到了以下链接: this one 其中我发现了这个有趣的例子:

    $('label[for="var_1"]').show();
    $('label[for="var_2"]').show();
    $('label[for="var_3"]').show();

我需要有人来解释这段代码,就像在这里执行的一样。特别是表达式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))。如何产生最终结果......

提前致谢

1 个答案:

答案 0 :(得分:2)

请记住高中代数的“操作顺序”:

  • 括号
  • 指数
  • 乘法
  • 加成
  • 减法

始终首先评估括号。并且,在具有嵌套括号集的表达式的情况下,内部集合在外部集合之前被评估。

所以,用:

men(like(women))

首先使用传递给它的like变量的值调用women函数。

无论该函数返回什么,那么传递给men函数的参数。像这样:

men( result of calling like(women) )

而且,women(like(men))

是相同的想法,只是反转首先调用哪一个。

所以,让我们一步一步。

  • 首先要认识到menwomen非常重要 和alien函数实际上都是相同的。他们只是调用 无论你传递给他们并返回结果(在这种情况下) 将永远是另一个功能)。你可以推断这一切,因为它们都是 do是在输入参数的末尾添加一组括号。 因此,他们每个人都期望传递一个功能(因为 这是你唯一可以调用的东西。)

所以,让我们不那么关注那些函数和对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!"