简体   繁体   English

在 Javascript 对象上定义方法/函数

[英]Defining methods/functions on Javascript Objects

Is this:这是:

saveUpdate() {
   // Some code
}

The same as:与以下相同:

saveUpdate: function() {
   // Some code
}

Is there a best practice (ES6) way to define methods on objects (specifically, Vue.js components)?是否有最佳实践(ES6)方法来定义对象(特别是 Vue.js 组件)的方法?

More context: I am having trouble in my Vue.js application with the methods triggering properly in productions.更多上下文:我的 Vue.js 应用程序在生产中正确触发方法时遇到问题。 The methods I have defined seem to work fine in development - but once compiled for production they do not seem to behave in the same way.我定义的方法似乎在开发中运行良好——但一旦编译用于生产,它们的行为似乎并不相同。 I have read in the Vue.js documentation that all methods need to be defined as NEW separate functions, and I am now wondering if the way I am defining the methods;我在 Vue.js 文档中读到所有方法都需要定义为新的单独函数,我现在想知道我定义方法的方式; is actually not correct.实际上是不正确的。

broader example:更广泛的例子:

...,
methods: {
  saveUpdate() {
   // Some code
  }
},
...

should I be doing something like:我应该做类似的事情:

...,
methods: {
  saveUpdate: () => {
   // Some code
  }
},
...

What is the modern, best practice or accepted way to do this?什么是现代的、最佳实践或公认的方式来做到这一点? Or am I looking in the wrong place and my declarations are just fine the way they are?还是我找错地方了,而我的声明就跟它们一样?

Yes, this:是的,这个:

saveUpdate() {
   // Some code
}

Is syntactic sugar for:是语法糖:

saveUpdate: function() {
   // Some code
}

There's nothing different between the two bar the syntax.两者的语法没有什么不同。 If you want an arrow function, I believe you need to use the second form:如果你想要一个箭头函数,我相信你需要使用第二种形式:

saveUpdate: () => {
   // Some code in a lexical `this` scope (not useful in Vue computed).
}

Also note that the -> syntax is invalid - it's a fat arrow => .另请注意->语法无效 - 它是一个箭头=> Although as Phil pointed out in the comments, you most likely won't want to use arrow functions in a Vue project as you'd lose the binding to this which is how you interact with your component.尽管正如Phil在评论中指出的那样,您很可能不想在 Vue 项目中使用箭头函数,因为您将失去与 this 的绑定, this是您与组件交互的方式。

saveUpdate() {...} is ES6 shortcut for saveUpdate: function() {...} , so yes they are the same. saveUpdate() {...}saveUpdate: function() {...} ,所以是的,它们是相同的。 Since Vue applications are commonly transpiled, there is no reason to not use the first option.由于 Vue 应用程序通常被转译,因此没有理由不使用第一个选项。

If a function is reused within application, it can be declared separately:如果一个函数在应用程序中被重用,它可以单独声明:

export function saveUpdate() {...}

...

export default {
  methods: { saveUpdate }
}

Arrows shouldn't be used for functions that expect to access Vue instance as this .箭头不应用于期望以this方式访问 Vue 实例的函数。

As the documentation explains:正如文档所解释的:

All lifecycle hooks are called with their this context pointing to the Vue instance invoking it.所有的生命周期钩子都被调用,它们的 this 上下文指向调用它的 Vue 实例。

Don't use arrow functions on an options property or callback, such as created: () => console.log(this.a) or vm.$watch('a', newValue => this.myMethod()) .不要在选项属性或回调上使用箭头函数,例如created: () => console.log(this.a)vm.$watch('a', newValue => this.myMethod()) Since an arrow function doesn't have a this, this will be treated as any other variable and lexically looked up through parent scopes until found, often resulting in errors such as Uncaught TypeError: Cannot read property of undefined or Uncaught TypeError: this.myMethod is not a function .由于箭头函数没有 this,因此 this 将被视为任何其他变量并通过父作用域进行词法查找直到找到,通常会导致错误,例如Uncaught TypeError: Cannot read property of undefinedUncaught TypeError: this.myMethod is not a function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM