在Javascript中定义对象方法的两种方法有什么区别?

abh*_*ekp 1 javascript function object

在阅读Eric Elliot 撰写的这篇文章https://medium.com/javascript-scene/the-single-biggest-mistake-programmers-make-every-day-62366b432308时,我遇到了以下类型的对象方法定义.

var obj = {
  getX() {
    document.write('X');
  }
}

obj.getX(); // X
Run Code Online (Sandbox Code Playgroud)

它与以下类型的定义有何不同?

var obj = {
  getX: function getX() {
    document.write('X');
  }
}

obj.getX(); // X
Run Code Online (Sandbox Code Playgroud)

Jar*_*a X 5

第一种表示法是ES2015/ES6简写符号,不适用于safari或Internet Explorer

docs:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions

因为第一个是第二个的简写符号,这意味着就功能而言没有区别 - 但是,直到Internet Explorer已经死亡并且被埋没并且safari赶上(或者跟随Internet Explorer到达同一个坟墓)第一个表示法不建议面向公众的网站使用,因为语法更改不可能出现"shim"或"polyfill"

最重要的是,大多数"原生移动"浏览器不支持这个(不是谈论chrome/firefox for android等)