A function lives on its own:
一个函数独立存在:
const bark = () => {
console . log ( 'wof!' )
}
bark ()
or
要么
function bark () {
console . log ( 'wof!' )
}
bark ()
A method is a function assigned to an object property:
方法是分配给对象属性的函数:
const dog = {
bark : () => {
console . log ( 'wof!' )
},
}
dog . bark ()
The method can access the object properties, but only when it’s a regular function, not an arrow function:
该方法可以访问对象属性,但仅当它是常规函数而不是箭头函数时:
const dog = {
name : 'Roger' ,
bark : function () {
console . log ( `I am ${ this . name } . wof!` )
},
}
dog . bark ()
翻译自: https://flaviocopes.com/javascript-difference-method-function/