Copying Objects

In real life, you copy a cool idea or thing by copying everything it does and changing a few things here and there to make it even better. The thing you’re copying becomes the prototype for the new way of doing it. JavaScript handles copying objects in a similar way.

To describe another movie, we can copy the prototypical best_movie object by using Object.create:

 
var​ great_movie = Object.create(best_movie);
 
great_movie.aboutMe();
 
// => Star Wars, starring: Mark Hamill,Harrison Ford,Carrie Fisher

Object.create will create a new object with all the same properties and methods of the prototypical object we created earlier. So the new object, great_movie, has the same title and actors as the original best_movie. It also has ...

Get 3D Game Programming for Kids now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.