Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// from https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/es.md#Object-Literal-Extensions
// Computed property name
const prop = 'foo';
const o = {
[prop]: 'hey',
['b' + 'ar']: 'there',
};
console.log(o); // {foo: 'hey', bar: 'there'}
// Shorthand property
const foo = 1;
const bar = 2;
const o2 = {foo, bar};
console.log(o2); // {foo: 1, bar: 2}
// Method property
const clearSky = {
// Basically the same as clouds: function() { return 0; }.
clouds() { return 0; },
color() { return 'blue'; },
};
console.log(clearSky.color()); // 'blue'
console.log(clearSky.clouds()); // 0
// clang-format object_literal_extensions.js > object_literal_extensions_clang_formatted.js
// Computed property name
const prop = 'foo';
const o = {
[prop]: 'hey',
['b' +
'ar']: 'there', // NOTE: THIS IS WEIRD.
};
console.log(o); // {foo: 'hey', bar: 'there'}
// Shorthand property
const foo = 1;
const bar = 2;
const o2 = {
foo,
bar // NOTE: THIS IS ALSO WEIRD.
};
console.log(o2); // {foo: 1, bar: 2}
// Method property
const clearSky = {
// Basically the same as clouds: function() { return 0; }.
clouds() {
return 0;
},
color() {
return 'blue';
},
};
console.log(clearSky.color()); // 'blue'
console.log(clearSky.clouds()); // 0
// java -jar third_party/closure_compiler/compiler/compiler.jar --chrome_pass --language_in=ECMASCRIPT_2017 --language_out=ECMASCRIPT5_STRICT --compilation_level=SIMPLE_OPTIMIZATIONS --jscomp_error=checkTypes object_literal_extensions.js > object_literal_extensions_closure_copmiled.js
'use strict';var prop="foo",$jscomp$compprop0={},o=($jscomp$compprop0[prop]="hey",$jscomp$compprop0.bar="there",$jscomp$compprop0);console.log(o);var foo=1,bar=2,o2={foo:foo,bar:bar};console.log(o2);var clearSky={clouds:function(){return 0},color:function(){return"blue"}};console.log(clearSky.color());console.log(clearSky.clouds());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment