Javascript Interview Questions: Javascript is a prototypal language. What do I mean by this?

William Gottschalk
EdgeCoders
3 min readJan 14, 2016

--

It’s a new year and you’re looking for that job that makes you excited to go to work. If you want that dream job you’re going to have to understand the fundamental features about your language. In Javascript, one of those features is the prototype chain.

What is a prototype?

A prototype is an object. When you declare a function, a prototype is created and linked to that function. In addition, the prototype object forms a link with its function creating a circular relationship.

A visual representation of a function and it’s prototype

This is true for any function. In javascript, we can create objects several different ways. One of the ways is the new keyword. When we declare a function with the intent of using new (a constructor function), we capitalize the first letter of the function name.

How does the new keyword work?

The new keyword has four steps:

  1. Create a new object
  2. Link the object to its prototype, naming that link __proto__
  3. Execute the constructor function, setting the context (this) to the new object
  4. Return the created object.

Lets write a function to mimic the new keyword

This is a visualization using pythontutor.com. The outcome between creating Mom and Dad is identical

The Prototype Chain

When you access a property or method in Javascript. Javascript will check the object to see if that property exists. If it doesn’t it will check it’s prototype and will continue to walk up the prototype chain until it reaches the Object prototype. The linkages between prototype objects are formed via the __proto__ property.

But it’s 2016… Aren’t you forgetting something

In ES2015 / ES6, the class keyword comes to Javascript. You can write the same exact code as above using a different syntax:

Example of object creation with ES2015 class keyword

The new keyword can be considered dangerous because it has the responsibility of setting this to the proper context. If you forget to use the new keyword, then this is bound to the global object causing potential bugs in your program.

Object.create

One solution to the new keyword issue is to add checks to your program every time you write a constructor to ensure the context of this is correctly assigned. Another way is to use Object.create. Object.create does the first 2 steps that the new keyword does and doesn’t have to touch this.

No problems with the new keyword
The prototype chain is correctly assigned.

But is there a better way?

The new keyword comes from languages like C# and Java that use classes and thus follow a classical inheritance model. In Javascript, classes don’t exist. Using things like new and class are ways to make Javascript feel more familiar to developers coming from class based languages. This can be a problem because inheritance doesn’t work the way it does in classical languages.

Under the hood, Javascript just uses plain objects and creates links between those objects to form new relationships. This allows for much more flexible and powerful ways to compose and inherit from other objects. This leads us int0 our next Javascript interview question: “How can you inherit properties from other objects?”

--

--

William Gottschalk
EdgeCoders

I’m a fullstack software engineer at Originate. Currently living in Los Angeles, CA. linkedin.com/in/willgottschalk