Method vs Functions, and other questions

前端 未结 8 1648
梦毁少年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条回答
  •  情深已故
    2020-11-27 13:47

    A method is a property of an object whose value is a function. Methods are called on objects in the following format: object.method().

    //this is an object named developer

     const developer = {
      name: 'Andrew',
      sayHello: function () {
        console.log('Hi there!');
      },
      favoriteLanguage: function (language) {
        console.log(`My favorite programming language is ${language}`);
      }
    };
    

    // favoriteLanguage: and sayHello: and name: all of them are proprieties in the object named developer

    now lets say you needed to call favoriteLanguage propriety witch is a function inside the object..

    you call it this way

    developer.favoriteLanguage('JavaScript');
    
    // My favorite programming language is JavaScript'
    

    so what we name this: developer.favoriteLanguage('JavaScript'); its not a function its not an object? what it is? its a method

提交回复
热议问题