为什么要分配字符串'原型'到一个变量,然后使用它来设置对象的原型

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

本文关键字:原型 然后 对象 变量 设置 字符串 分配 为什么 一个      更新时间:2023-09-26

我刚刚遇到了一个classList polyfill的代码,我不明白开发人员为什么选择这样做:

var protoProp = "prototype";

在代码的后面。。。

ClassList[protoProp] = [];

ClassList.prototype = []出了什么问题?我想不出他们为什么要这么做。我错过了什么?

它看起来是最小化的,可以将该值转换为

ClassList[v]=[];

而不是

ClassList.prototype = [];

protoProp变量在多个位置使用,而不是prototype,并且将在总中节省几个字节

在我的上一个项目中,我需要缩小&混淆我的代码,我转换所有像这样的对象

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

每件事都做得很好现在我有了弦lengthreplaceprototypecolorize我们有多种方法来混淆这些字符串,使其更难阅读。