From aa716b40d12cc6178da56e795f2c8ce2cbf0c998 Mon Sep 17 00:00:00 2001 From: KurtWayn3 Date: Sun, 17 Feb 2019 10:43:24 -0700 Subject: [PATCH] Add Information on Object Initializing in ES6 (#32835) * Add Information on Object Initializing in ES6 * fix: added front matter block --- .../es6/object-initializer/index.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 guide/english/javascript/es6/object-initializer/index.md diff --git a/guide/english/javascript/es6/object-initializer/index.md b/guide/english/javascript/es6/object-initializer/index.md new file mode 100644 index 0000000000..c533622f47 --- /dev/null +++ b/guide/english/javascript/es6/object-initializer/index.md @@ -0,0 +1,44 @@ +--- +title: ES6 Object Initializers +--- + +### ES6 Object Initializers + +In ES6 you can use shorthand property syntax to initialize objects, which at first does not appear to be very time-saving but when you are creating many objects in a project can be super useful. + +If you have a property name that is exactly the same as your variable name instead of placing it twice in ES6 you can use the shorthand property syntax: +```javascript +// ES5 +var name = 'Lord Farquad'; + +var user = { + name: name, + }; + + // ES6 + const name = 'Shrek'; + + const user = { + name, + }; +``` +This shorthand property syntax is also useful when initializing methods in an objec: +```javascript +// ES5 +var example = { + getName: function(name) { + return name.firstName + ' ' + name.lastName; + }, +}; + +// ES6 Shorthand +const example = { + getName(name) { + return name.firstName + ' ' + name.lastName; + }, +}; +``` + +[Learn More about Object Initializers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer) + +