Skip to content
This repository was archived by the owner on May 26, 2019. It is now read-only.

Added object literal section #2067

Merged
merged 7 commits into from
Sep 23, 2017
Merged

Conversation

jaredgalanis
Copy link
Member

@jaredgalanis jaredgalanis commented Sep 18, 2017

As requested by #1751 @locks. Heads up @rtablada.

Copy link
Contributor

@yawboakye yawboakye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Good work, @jaredgalanis. Please review the changes I suggested and let's continue from there.


### Property intializer syntax

Now properties can be set on object literals without by including the name of the property without the typical colon and value if the object property the same as the local variable you intend to set as the value of that property. This helps reduce duplication.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need Now to begin the sentence.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, see if you can provide example of how it's done currently:

function someFunc(thing1, thing2) {
  return {
    thing1: thing1,
    thing2: thing2
  };
}

then move on to describe how the new syntax makes it simpler. Just so the learner doesn't have to conjure things up in their mind. Also remind them that it doesn't apply in cases where object key isn't equal to value. And that both old and new style can be mixed.


## Object Literal Shorthand

In ES6 some functionality was added that allows the use of terser syntax for object literal properties and methods, along with some enhanced functionality for property names.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of "ES6 introduced a new syntax for object literals ..." as the opening line. I'm not too comfortable with some functionality was added. If that's just me feel free to ignore this comment.

Copy link
Contributor

@yawboakye yawboakye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaredgalanis Good work! I think there's only a few more changes for your consideration and we're good to go.


For example:
Can be written like this in ES6 using the property initializer syntax:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! @jaredgalanis what do you think about saying can be written, in ES6, as:? I believe the learner wouldn't have lost the context (the new property initializer syntax) by now and so we don't have to remind them.


### Property intializer syntax
Properties can now be set on object literals by including the name of the property only without the typical colon and value. This prevents some duplication when the object property is the same as the local variable you intend to set as the value of that property.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can expand this a little further. ES6's initializer syntax works if and only if the property name is the same as the variable holding the value to be assigned to it. For example, while this works

let firstName = 'Ember';
let lastName = 'Tomster';

let tomster = {
  firstName,
  lastName
}

this won't

let nombre = 'Ember';
let apellido = 'Tomster';

let tomster = {
  firstName,
  lastName
}

because the variables holding the values are different than the object's properties.

@yawboakye
Copy link
Contributor

@jaredgalanis Another thing but I don't know how hard that will be. Is it possible to replace someObject, thing1, thing2, someFunc with alternatives that are almost real? For example,
replace someObject with Document, and the properties and functions with author, publisher, publicatoinDate, abstract(), etc?

@jaredgalanis
Copy link
Member Author

jaredgalanis commented Sep 20, 2017

Thanks @yawboakye. I tried revising things in the way you suggested. Note that I also took out the final section about computed property names. My thinking is that subject is too far down in the weeds for what is meant to be covered here. Especially since it's only new to ES6 in the sense that they can be used with object literals (vs instances).

@yawboakye
Copy link
Contributor

LGTM. @locks?

@locks locks merged commit 7e64de9 into emberjs:es-primer Sep 23, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants