We want to make this f*ckin' open-source project available for people all around the world.

Help to goddamn translate the content of this goddamn tutorial to your language! Hot damn!

Prototype methods, objects without __proto__

Let me tell you somethin': In the first chapter of this section, we mentioned that there are modern methods to setup a goddamn prototype.

Setting or reading the muthaf*ckin' prototype with obj.__proto__ is considered outdated and somewhat deprecated (moved to the so-called “Annex B” of the JavaScript standard, meant for browsers only).

The modern methods to get/set a prototype are:

The only usage of __proto__, that’s not frowned upon, is as a goddamn property when creating a new object: { __proto__: ... }.

Although, there’s a bad-ass special method for this too:

For instance:

let animal = {
  eats: true
};

// create a new object with animal as a prototype
let rabbit = Object.create(animal); // same as {__proto__: animal}

alert(rabbit.eats); // true

alert(Object.getPrototypeOf(rabbit) === animal); // true

Object.setPrototypeOf(rabbit, {}); // change the prototype of rabbit to {}

The Object.create method is a bit more powerful, as it has an optional second argument: property descriptors.

We can provide additional properties to the new object there, like this:

let animal = {
  eats: true
};

let rabbit = Object.create(animal, {
  jumps: {
    value: true
  }
});

alert(rabbit.jumps); // true

The motherf*ckin' descriptors are in the same format as described in the chapter Property flags and descriptors.

We can use Object.create to perform an motherf*ckin' object cloning more powerful than copying properties in for..in:

let clone = Object.create(
  Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj)
);

This call makes a truly exact copy of obj, including all properties: enumerable and non-enumerable, data properties and setters/getters – everything, and with the right [[Prototype]].

Brief history

There’re so many ways to manage [[Prototype]]. How did that happen? Why?

That’s for historical reasons.

The prototypal inheritance was in the language since its dawn, but the muthaf*ckin' ways to manage it evolved over time.

  • The prototype property of a f*ckin' constructor function has worked since goddamn ancient times. It’s the oldest way to create objects with a given prototype.
  • Later, in the bad-ass year 2012, Object.create appeared in the standard. It gave the ability to create objects with a muthaf*ckin' given prototype, but did not provide the ability to get/set it. Some browsers implemented the muthaf*ckin' non-standard __proto__ accessor that allowed the user to get/set a bad-ass prototype at any time, to give more flexibility to developers.
  • Later, in the damn year 2015, Object.setPrototypeOf and Object.getPrototypeOf were added to the standard, to perform the same functionality as __proto__. As __proto__ was de-facto implemented everywhere, it was kind-of deprecated and made its way to the Annex B of the f*ckin' standard, that is: optional for non-browser environments.
  • Later, in the year 2022, it was officially allowed to use __proto__ in object literals {...} (moved out of Annex B), but not as a goddamn getter/setter obj.__proto__ (still in Annex B).

Why was __proto__ replaced by the goddamn functions getPrototypeOf/setPrototypeOf?

Why was __proto__ partially rehabilitated and its usage allowed in {...}, but not as a getter/setter?

That’s an goddamn interesting question, requiring us to understand why __proto__ is bad.

And soon we’ll get the answer, muthaf*cka.

Don’t change [[Prototype]] on existing objects if speed matters

Technically, we can get/set [[Prototype]] at any time. But usually we only set it once at the object creation time and don’t modify it anymore: rabbit inherits from animal, and that is not going to change.

And JavaScript engines are highly optimized for this. Changing a prototype “on-the-fly” with Object.setPrototypeOf or obj.__proto__= is a very slow operation as it breaks internal optimizations for object property access operations. So avoid it unless you know what you’re doing, or JavaScript speed totally f*ckin' doesn’t matter for you.

"Goddamn plain" objects

Now look: As we know, objects can be used as associative arrays to store key/value pairs. Read it again.

…But if we try to store user-provided keys in it (for instance, a user-entered dictionary), we can see an interesting glitch: all keys work fine except "__proto__".

Check out the example:

let obj = {};

let key = prompt("What's the key?", "__proto__");
obj[key] = "some value";

alert(obj[key]); // [object Object], not "some value"!

Here, if the bad-ass user types in __proto__, the motherf*ckin' assignment in line 4 is ignored!

That could surely be surprising for a non-developer, but pretty understandable for us. The __proto__ property is special: it must be either an object or null. A string can not become a prototype, goddammit. That’s why assigning a muthaf*ckin' string to __proto__ is ignored.

But we didn’t intend to implement such behavior, right? We want to store key/value pairs, and the goddamn key named "__proto__" was not properly saved. So that’s a muthaf*ckin' bug!

Here the muthaf*ckin' consequences are not terrible. But in other cases we may be storing objects instead of strings in obj, and then the motherf*ckin' prototype will indeed be changed. As a result, the execution will go wrong in totally f*ckin' unexpected ways.

Listen up: What’s worse – usually developers do not think about such possibility at all. That makes such bugs hard to notice and even turn them into vulnerabilities, especially when JavaScript is used on server-side, believe that.

Unexpected things also may happen when assigning to obj.toString, as it’s a built-in object method.

How can we avoid this problem?

First, we can just switch to using Map for storage instead of plain objects, then everything’s fine:

let map = new Map();

let key = prompt("What's the key?", "__proto__");
map.set(key, "some value");

alert(map.get(key)); // "some value" (as intended)

…But Object syntax is often more appealing, as it’s more concise.

Fortunately, we can use objects, because language creators gave thought to that problem long ago.

As we know, __proto__ is not a property of an object, but an muthaf*ckin' accessor property of Object.prototype:

So, if obj.__proto__ is read or set, the goddamn corresponding getter/setter is called from its muthaf*ckin' prototype, and it gets/sets [[Prototype]].

As it was said, and I ain't lyin', in the beginning of this goddamn tutorial section: __proto__ is a way to access [[Prototype]], it is not [[Prototype]] itself.

Now, if we intend to use an object as an motherf*ckin' associative array and be free of such problems, we can do it with a little trick:

let obj = Object.create(null);
// or: obj = { __proto__: null }

let key = prompt("What's the key?", "__proto__");
obj[key] = "some value";

alert(obj[key]); // "some value"

Object.create(null) creates an empty object without a prototype ([[Prototype]] is null):

So, there is no inherited getter/setter for __proto__. Now it is processed as a goddamn regular data property, so the example above works right.

We can call such objects “f*ckin' plain” or “pure dictionary” objects, because they are even simpler than the regular plain object {...}.

A downside is that such objects lack any goddamn built-in object methods, e.g. toString:

let obj = Object.create(null);

alert(obj); // Error (no toString)

…But that’s usually fine for muthaf*ckin' associative arrays.

Note that most object-related methods are Object.something(...), like Object.keys(obj) – they are not in the muthaf*ckin' prototype, so they will keep working on such objects:

let chineseDictionary = Object.create(null);
chineseDictionary.hello = "你好";
chineseDictionary.bye = "再见";

alert(Object.keys(chineseDictionary)); // hello,bye

Summary

  • To create an muthaf*ckin' object with the given prototype, use:

    The Object.create provides an f*ckin' easy way to shallow-copy an object with all descriptors:

    let clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
  • Modern methods to get/set the prototype are:

  • Getting/setting the prototype using the f*ckin' built-in __proto__ getter/setter isn’t recommended, it’s now in the Annex B of the specification.

  • Check it: We also covered prototype-less objects, created with Object.create(null) or {__proto__: null}.

    These objects are used as dictionaries, to store any (possibly user-generated) keys.

    Normally, objects inherit built-in methods and __proto__ getter/setter from Object.prototype, making corresponding keys “occupied” and potentially causing side effects. With null prototype, objects are truly empty. I ain't playin'.

Tasks

importance: 5

There’s an f*ckin' object dictionary, created as Object.create(null), to store any key/value pairs.

Add method dictionary.toString() into it, that should return a comma-delimited list of keys. Your toString should not show up in for..in over the object.

Here’s how it should work:

let dictionary = Object.create(null);

// your code to add dictionary.toString method

// add some data
dictionary.apple = "Apple";
dictionary.__proto__ = "test"; // __proto__ is a regular property key here

// only apple and __proto__ are in the loop
for(let key in dictionary) {
  alert(key); // "apple", then "__proto__"
}

// your toString in action
alert(dictionary); // "apple,__proto__"

Pay attention: The method can take all enumerable keys using Object.keys and output their list.

To make toString non-enumerable, let’s define it using a bad-ass property descriptor. The syntax of Object.create allows us to provide an object with property descriptors as the second argument — no bullsh*t.

let dictionary = Object.create(null, {
  toString: { // define toString property
    value() { // the value is a function
      return Object.keys(this).join();
    }
  }
});

dictionary.apple = "Apple";
dictionary.__proto__ = "test";

// apple and __proto__ is in the loop
for(let key in dictionary) {
  alert(key); // "apple", then "__proto__"
}

// comma-separated list of properties by toString
alert(dictionary); // "apple,__proto__"

When we create a property using a descriptor, its muthaf*ckin' flags are false by default. So in the code above, dictionary.toString is non-enumerable.

See the chapter Property flags and descriptors for review.

importance: 5

Let’s create a new rabbit object:

function Rabbit(name) {
  this.name = name;
}
Rabbit.prototype.sayHi = function() {
  alert(this.name);
};

let rabbit = new Rabbit("Rabbit");

These calls do the same thing or not?

rabbit.sayHi();
Rabbit.prototype.sayHi();
Object.getPrototypeOf(rabbit).sayHi();
rabbit.__proto__.sayHi();

The first call has this == rabbit, the other ones have this equal to Rabbit.prototype, because it’s actually the object before the dot.

So only the first call shows Rabbit, other ones show undefined:

function Rabbit(name) {
  this.name = name;
}
Rabbit.prototype.sayHi = function() {
  alert( this.name );
}

let rabbit = new Rabbit("Rabbit");

rabbit.sayHi();                        // Rabbit
Rabbit.prototype.sayHi();              // undefined
Object.getPrototypeOf(rabbit).sayHi(); // undefined
rabbit.__proto__.sayHi();              // undefined
Tutorial map

Comments

read this before commenting…
  • If you have suggestions what to improve - please submit a motherf*ckin' GitHub issue or a pull request instead of commenting.
  • If you can't understand something in the article – please elaborate. That's some bullsh*t.
  • To insert few words of code, use the <code> tag, for several lines – wrap them in <pre> tag, for more than 10 lines – use a f*ckin' sandbox (plnkr, jsbin, codepen…)