JS 对象文字中的 'method-like' 语法是什么
What is this 'method-like' syntax in JS object literal
使用ES6 class糖,我们可以这样定义函数:
class Foo {
constructor(props) {}
...
myFn() {}
}
而在 JS 对象字面量中,我们可以这样定义 getters 和 setters:
foo = {
get data() {}
set data(val) {}
}
然而,这是什么语法:
foo = {
data() {}
}
这个对象是数据属性吗?或者 getters/setters?
注意:此语法在 Vue.js 2.0 文档中广泛使用,例如新添加的 render
函数。
new Vue({
render (h) {
throw new Error('oops')
},
renderError (h, err) {
return h('pre', { style: { color: 'red' }}, err.stack)
}
}).$mount('#app')
它只是 shorthand。 refer to MDN docs
// Shorthand method names (ES2015)
var o = {
// doesnt need a colon!
property([parameters]) {},
get property() {},
set property(value) {}
};
foo = {
data() {}
}
是
的简写
foo = {
data: function() {}
}
使用ES6 class糖,我们可以这样定义函数:
class Foo {
constructor(props) {}
...
myFn() {}
}
而在 JS 对象字面量中,我们可以这样定义 getters 和 setters:
foo = {
get data() {}
set data(val) {}
}
然而,这是什么语法:
foo = {
data() {}
}
这个对象是数据属性吗?或者 getters/setters?
注意:此语法在 Vue.js 2.0 文档中广泛使用,例如新添加的 render
函数。
new Vue({
render (h) {
throw new Error('oops')
},
renderError (h, err) {
return h('pre', { style: { color: 'red' }}, err.stack)
}
}).$mount('#app')
它只是 shorthand。 refer to MDN docs
// Shorthand method names (ES2015)
var o = {
// doesnt need a colon!
property([parameters]) {},
get property() {},
set property(value) {}
};
foo = {
data() {}
}
是
的简写foo = {
data: function() {}
}