Why assign the string 'prototype' to a variable and then use it to set the prototype of an object?

I just came across this code for classList polyfill , and I don't understand why the developer decided to do the following:

var protoProp = "prototype";

... later in code ...

ClassList[protoProp] = [];

What happened to ClassList.prototype = []? I can’t think of why they will do it. What am I missing?

+4
source share
2 answers

It looks to minimize where this value can be converted to

ClassList[v]=[];

but not

ClassList.prototype = [];

The variable protoPropuses several places instead prototypeand saves only a few bytes

+4
source

, ,

arr.length ===> arr["length"];
str.replace("a","") ===> str["replace"]("a","");
Element.prototype.colorize ===> Element["prototype"]["colorize"]

length, replace, prototype, colorize , .

0

Source: https://habr.com/ru/post/1658341/


All Articles