Copying Objects

In real life, you copy a cool idea or an interesting device by duplicating 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 bestMovie object with Object.create().

​ ​var​ greatMovie = Object.create(bestMovie);
​ greatMovie.logMe();
​ ​// => Star Wars, starring: Mark Hamill,Harrison Ford,Carrie Fisher​

Object.create() will create a new object with all the same properties and methods as the prototypical object we created earlier. So the new object, greatMovie, has the same title and actors as the ...

Get 3D Game Programming for Kids, 2nd Edition 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.