MrMisterMan MrMisterMan - 1 year ago 76
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 Source

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


rather than

ClassList.prototype = [];

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download