How to differentiate between a method and a function?

This question already has an answer here:

  • Difference between a method and a function 33 answers

  • Methods are simply function references stored in an object property. Method in Javascript is merely a concept, not actually an existing syntactical part. Also, there is no method keyword in Javascript.

    function foo() { /* whatever */ }
    
    var bar = {};
    bar.baz = foo;
    
    // You'd consider this a function call
    foo();
    
    // While the following is actually syntactically also a function call
    // you might consider baz a "method of the bar object"
    bar.baz();
    // and thus bar.baz() would then be a "method call".
    

    Please not that to access a property of an object you have two possible ways: object.property and object[propertyName] where propertyName is a string containing the name of the property.

    So

    bar.baz()
    

    achieves exactly the same as and is identical to

    bar["baz"]().
    
    链接地址: http://www.djcxy.com/p/17318.html

    上一篇: 构造函数中的方法和函数

    下一篇: 如何区分方法和函数?