我们应该始终保留JavaScript内置对象方法的副本吗?

时间:2023-01-15 20:51:15

There are loads of functions defined in JavaScript built-in object. For example String has slice(),split() and Array has join() and they can be overridden (though it is a bad practice) by simply using

JavaScript内置对象中定义了大量函数。例如,String有slice(),split()和Array有join(),只需使用它们就可以覆盖它们(虽然这是一个不好的做法)

Array.prototype.join = 'test', Then all the array will lose the join() method

Array.prototype.join ='test',然后所有数组都将丢失join()方法

However, as a developer of open source project, in what way should we view this potential changes to the prototype? It will become a problem because it is very likely we will use these functions more or less to achieve some functionalities.

但是,作为开源项目的开发者,我们应该以何种方式查看原型的这种潜在变化?这将成为一个问题,因为我们很可能会或多或少地使用这些功能来实现某些功能。

In jQuery, it will keep a copy of some of those methods:

在jQuery中,它将保留一些这些方法的副本:

var class2type = {}; var toString = class2type.toString; var hasOwn = class2type.hasOwnProperty;

var class2type = {}; var toString = class2type.toString; var hasOwn = class2type.hasOwnProperty;

But only some of the methods are kept safe, if you read through the source code of jQuery you will notice some String methods are called directly on the String object.

但是只有一些方法是安全的,如果你仔细阅读jQuery的源代码,你会发现一些String方法直接在String对象上调用。

Of course we can always keep a copy of all the methods of built-in JavaScript object but it is so messy. Indeed, it also becomes a problem of execution timing. If the program is executed before the prototype is changed, then it's fine. But it will fail if it is vice versa. So I am more interested to know when jQuery or other libraries they decide to keep a copy of some of the methods, what are their philosophy behind the decision?

当然,我们可以随时保留内置JavaScript对象的所有方法的副本,但它非常混乱。实际上,它也成为执行时间的问题。如果程序在原型发生变化之前执行,那就没关系了。但如果相反,它将会失败。所以我更感兴趣的是,当他们决定在jQuery或其他库中保留一些方法的副本时,他们的决定背后的哲学是什么?

Thank you.

1 个解决方案

#1


2  

As a developer of an open source project, the safest and best thing to do is to pretend that the builtin prototypes are frozen. The practice of extending or modifying them is responsible for far more problems than solutions.

作为开源项目的开发人员,最安全和最好的做法是假装内置原型被冻结。扩展或修改它们的做法导致的问题远远超过解决方案。

If you need a custom join function, just create it so that it takes a string as the first argument.

如果您需要自定义连接函数,只需创建它,以便它将字符串作为第一个参数。

function join(string, separator) {
  // ... 
}

This keeps the prototypes clean and stops you from having to worry about references to overwritten methods.

这样可以保持原型的清洁,并使您不必担心对覆盖方法的引用。

What you're seeing there in the jQuery example is just aliasing. The methods are being assigned to variables to make them easier to use.

你在jQuery示例中看到的只是别名。这些方法被分配给变量以使它们更易于使用。

There's no way to code defensively around this, because by the time jQuery loads another script could have already modified the builtin prototypes and the original references would be lost.

没有办法对此进行防御性编码,因为当jQuery加载另一个脚本时,可能已经修改了内置原型并且原始引用将丢失。

#1


2  

As a developer of an open source project, the safest and best thing to do is to pretend that the builtin prototypes are frozen. The practice of extending or modifying them is responsible for far more problems than solutions.

作为开源项目的开发人员,最安全和最好的做法是假装内置原型被冻结。扩展或修改它们的做法导致的问题远远超过解决方案。

If you need a custom join function, just create it so that it takes a string as the first argument.

如果您需要自定义连接函数,只需创建它,以便它将字符串作为第一个参数。

function join(string, separator) {
  // ... 
}

This keeps the prototypes clean and stops you from having to worry about references to overwritten methods.

这样可以保持原型的清洁,并使您不必担心对覆盖方法的引用。

What you're seeing there in the jQuery example is just aliasing. The methods are being assigned to variables to make them easier to use.

你在jQuery示例中看到的只是别名。这些方法被分配给变量以使它们更易于使用。

There's no way to code defensively around this, because by the time jQuery loads another script could have already modified the builtin prototypes and the original references would be lost.

没有办法对此进行防御性编码,因为当jQuery加载另一个脚本时,可能已经修改了内置原型并且原始引用将丢失。