在使用纯JavaScript时,我注意到有些人会将他们的对象定义为:
var oddObject = {
someProperty: 'random1',
// NOTE: No function keyword. Why does this work and are there
// other places this is also not required?
someFunction() { return "boring value"; },
someOtherProperty: 'random2'
}
var el = document.getElementById("result");
el.innerHTML = oddObject.someFunction();
我制作了一个fiddle,以防您真的想看到它可以工作(至少在Chrome 47中是这样)。那么为什么允许这样做?还有其他可以利用缩写的地方吗?