Method vs Functions, and other questions

前端 未结 8 1622
梦毁少年i
梦毁少年i 2020-11-27 13:26

With respect to JS, what\'s the difference between the two? I know methods are associated with objects, but am confused what\'s the purpose of functions? How does the syntax

相关标签:
8条回答
  • Your first line, is creating an object that references a function. You would reference it like this:

    myFirstFunc(param);
    

    But you can pass it to another function since it will return the function like so:

    function mySecondFunction(func_param){}
    mySecondFunction(myFirstFunc);
    

    The second line just creates a function called myFirstFunc which would be referenced like this:

    myFirstFunc(param);
    

    And is limited in scope depending on where it is declared, if it is declared outside of any other function it belongs to the global scope. However you can declare a function inside another function. The scope of that function is then limited to the function its declared inside of.

    function functionOne(){
        function functionTwo(){}; //only accessed via the functionOne scope!
    }
    

    Your final examples are creating instances of functions that are then referenced though an object parameter. So this:

    function myFirstFunc(param){};
    
    obj.myFirst = myFirstFunc(); //not right!
    obj.myFirst = new myFirstFunc(); //right!
    
    obj.myFirst('something here'); //now calling the function
    

    Says that you have an object that references an instance of a function. The key here is that if the function changes the reference you stored in obj.myFirst will not be changed.

    While @kevin is basically right there is only functions in JS you can create functions that are much more like methods then functions, take this for example:

    function player(){
    
        this.stats = {
            health: 0,
            mana: 0,
    
            get : function(){
                return this;
            },
    
            set : function( stats ){
                this.health = stats.health;
                this.mana = stats.mana;
            }  
    }    
    

    You could then call player.stats.get() and it would return to you the value of heath, and mana. So I would consider get and set in this instance to be methods of the player.stats object.

    0 讨论(0)
  • 2020-11-27 14:08
    var myFirstFunc = function(param) {
        //Do something
    };
    

    and

    function myFirstFunc(param) {
        //Do something
    };
    

    are (almost) identical. The second is (usually) just shorthand. However, as this jsfiddle (http://jsfiddle.net/cu2Sy/) shows, function myFirstFunc will cause the function to be defined as soon as the enclosing scope is entered, whereas myFirstFunc = function will only create it once execution reaches that line.

    As for methods, they have a this argument, which is the current object, so:

    var obj = {};
    obj.func = function( ) {
        // here, "this" is obj
        this.test = 2;
    }
    console.log( obj.test ); // undefined
    obj.func( );
    console.log( obj.test ); // 2
    

    The exact syntax you showed is because you can also do this:

    function abc( ) {
        this.test = 2;
    }
    var obj = {};
    obj.func = abc;
    obj.func( ); // sets obj.test to 2
    

    but you shouldn't without good reason.

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