MrMisterMan MrMisterMan - 1 month ago 5
Javascript Question

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

I just came across this code for a classList polyfill and I can't understand why the developer chose to do the following:

var protoProp = "prototype";


... later in the code...

ClassList[protoProp] = [];


What's wrong with
ClassList.prototype = []
? I can't think of any reason why they would do this. What am I missing?

Answer

It looks to be for minimisation, where that value can be converted into

ClassList[v]=[];

rather than

ClassList.prototype = [];

The protoProp variable is used several places instead of prototype and would save a few bytes in total