What is meant by 'first class object'?

后端 未结 11 1373
生来不讨喜
生来不讨喜 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 11:13

    In javascript functions are first class objects because it can do a lot more than what objects can do.

    • A function is an instance of an Object type.

    Function instanceof Object //returns true

    Like an object a function can have properties and can have a link back to it’s constructor function.

     var o = {}; // empty object 'o'
        o.a = 1 ; 
        o.b = 2 ; 
    
        console.log(o.a); // 1
        console.log(o.b); // 2 
    
    
        function foo(){};
        foo.a = 3 ; 
        foo.b = 4 ; 
    
        console.log(foo.a);  // logs 3
        console.log(foo.b);  // logs 4 

    • Functions can be stored in a variable as a value.

      var foo = function(){}; 
        console.log(foo); // function(){}

    • Functions can be passed as arguments to other functions

    function callback (foo){
          foo();
    }
    
    callback(function(){console.log('Successfuly invoked as an argument inside function callback')})

    • You can return a function from a function

     function foo(){
    	    return function(){console.log('working!')};
        }
    
        var bar = foo();
        bar(); // working!

    • Can be stored in a variable as a reference.

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

提交回复
热议问题