Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Object Enhancements in ES6


    Building on the ES6 Shorthand Properties lesson, John shows the other new Object Enhancements in ES6 that will help you write less code and easily create complex Objects.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    ES6 comes with some object enhancements. If I want to create a car with that color and speed, I can just say "color, speed" and then log out car.color and car.speed. When I run this, you can see I get red and 10.

    That's because if you declare a property with the exact same name as something, it'll treat it just as if you did "color:color" and "speed:speed." This is the ES5 way, and this is the ES6 way.

    This even works for functions. If I create a function called "go" and say "vroom," I can simply add another property here, call it "go," and then say, "car.go." Then I'll run this. You can see it says, "vroom."

    Another cool thing...I'm going to format this just a little bit, put these on new that instead of using the function keyword, I can actually cut and paste this here. Then I'll just delete that.

    This is valid syntax for declaring a function on an object. If I save and run again, you'll see I get the same result. This is the ES6 way, and that's the ES5 way. It's basically a shorthand that saves you some typing.

    The last fun trick to show you is that if you type something like this, you can actually have a computed property where it'll evaluate this. This is pretty much just like if you were to do car"go" like that, but now you're doing it inline inside of an object declaration.

    This will still work when I run it. I ran it and got vroom still. I can actually extract this go. I'll cut that out, say "var drive," paste it in there. Then we can put drive in here, hit save, run it. We still get vroom.

    You could do any sort of string concatenation or evaluation in there to generate some sort of string, which would evaluate into the name of something that you could then call later on.