JavaScript function declaration

前端 未结 8 794
别那么骄傲
别那么骄傲 2021-01-30 04:49

Are the JavaScript code snippets given below some sort of function declaration? If not can someone please give an overview of what they are?

some_func = function         


        
相关标签:
8条回答
  • 2021-01-30 04:56

    One way of doing it:

    var some_func = function(value) {  
        // some code here
    }
    

    Another way:

    function some_funct() {
    }
    

    Yet another way:

    var some_object={};
    some_object["some_func"] = function() {};
    

    or:

    var some_object={};
    some_object.some_func = function() {};
    

    In other words, they are many ways to declare a function in JS.


    Your second example is not correct.

    0 讨论(0)
  • 2021-01-30 04:58

    There are six ways/contexts in which to create functions:

    1) Standard declarative notation (most familiar to people with C background)

    function foo() {}
    

    All the rest are function expressions:

    2) As a method of an object literal

    var obj = {
        foo: function() {}
    };
    

    3) As a method of an instantiated object (created each time new is exectued)

    var Obj = function() {
        this.foo = function() {};
    };
    

    4) As a method of a prototype (created only once, regardless of how many times new is executed)

    var Obj = function() {};
    Obj.prototype.foo = function() {};
    

    5) As an anonymous function with a reference (same effect as #1) *

    var foo = function() {};
    

    6) As an immediately executed anonymous function (completely anonymous)

    (function() {})();
    

    * When I look at this statement, I consider the result. As such, I don't really consider these as anonymous, because a reference is immediately created to the function and is therefore no longer anonymous. But it's all the same to most people.

    0 讨论(0)
  • 2021-01-30 05:03

    The first one is simply creating an anonymous function and assigning it to a variable some_func. So using some_func() will call the function.

    The second one should be part of an object notation

    var obj = {
      show:function(value){
        // some code here
      }
    };
    

    So, obj.show() will call the function

    In both cases, you are creating an anonymous function. But in the first case, you are simply assigning it to a variable. Whereas in the second case you are assigning it as a member of an object (possibly among many others).

    0 讨论(0)
  • 2021-01-30 05:03

    The first one...

    some_func = function(value) {  
        // some code here
    }
    

    is declaring a variable and assigned an anonymous function to it, which is equivalent to...

    function some_func (value) {  
        // some code here
    }
    

    The second one should look like this...

    obj = {
        show:function(value){
           // some code here
        }
    }
    // obj.show(value)
    

    and equivalent to...

    //pseudo code
    class MyClass {
        function show (value) {
            // some code here
        }
    }
    obj = new MyClass();    // obj.show(value)
    

    Cheers

    0 讨论(0)
  • 2021-01-30 05:13

    They are called anonymous functions; you can read more about them here:

    http://www.ejball.com/EdAtWork/2005/03/28/JavaScriptAnonymousFunctions.aspx

    0 讨论(0)
  • 2021-01-30 05:17

    The first one is a function declaration assigned to a variable (at least it should be, despite the fact that it's missing the variable type declaration first), the second one is probably related to a object declaration.

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