What is meant by 'first class object'?

后端 未结 11 1367
生来不讨喜
生来不讨喜 2020-11-22 10:46

In a recent question, I received suggestions to talk on, amongst other things, the aspect of JavaScript where functions are \'first class\' objects. What does the \'first c

相关标签:
11条回答
  • 2020-11-22 10:52

    More complete approval of Strachey-Sussman-Abelson's formulation. So if your language supports such a construct then you've got a function as a first-class language :)

    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!" :)
    
    //women(like(aliens)); //  Who knows? Life is sometimes so unpredictable... :)
    

    In short, anything is a first-class object if it acts in the language as a state manipulation sort of object or type of object. Simply something you can operate on and pass around statements and evaluate in expressions at the same time. Or even shorter: when you can think of a function as an object that can be additionally invoked.

    0 讨论(0)
  • 2020-11-22 10:53

    To quote Wikipedia:

    In computer science, a programming language is said to support first-class functions (or function literal) if it treats functions as first-class objects. Specifically, this means that the language supports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning them as the values of other functions.

    This page also illustrates it beautifully:

    Really, just like any other variable

    • A function is an instance of the Object type
    • A function can have properties and has a link back to its constructor method
    • You can store the function in a variable
    • You can pass the function as a parameter to another function
    • You can return the function from a function

    also read TrayMan's comment, interesting...

    0 讨论(0)
  • 2020-11-22 10:56

    JavaScript functions are first-class functions meaning functions and objects are treated as the same thing. Functions can be stored as a variable inside an object or an array as well as it can be passed as an argument or be returned by another function. That makes function "first-class citizens in JavaScript"

    JavaScript uses literal notation syntax which makes it hard to fully grasp the fact that in JavaScript functions are objects.

    For example..

    var youObj1 = new Object();
    // or
    var youObj1 = {};
    

    both declerations are equivalent. By using new we are calling the constructor function of an Object. Also by using {} (JavaScript shortcut called literals) we are calling the construction function of an Object. {} is just a shorter representation for instantiating the constructor.

    Most languages uses new keyword to create an object, so lets create a JavaScript object.

    var myFunction = new Function("a",  "b", 'return a_b');
    

    As you see we created an object name function.

    Creating same object name function using JavaScript function expression..

    var myFunction = function myFunction(a,b) {
        return a+b;
    }
    

    Here we go we just created a object name function.

    0 讨论(0)
  • 2020-11-22 11:00

    i guess when something is first class in a language, it means that it's supported by its syntax rather than a library or syntactic sugar. for example, classes in C are not first class

    0 讨论(0)
  • 2020-11-22 11:01

    It means that functions are objects, with a type and a behaviour. They can be dynamically built, passed around as any other object, and the fact that they can be called is part of their interface.

    0 讨论(0)
  • 2020-11-22 11:02

    Simple test. If you can do this in your language (Python as example):

    def double(x):
        return x*x
    
    f = double
    
    print f(5) #prints 25
    

    Your language is treating functions as first class objects.

    0 讨论(0)
提交回复
热议问题