为什么不赞成修改JavaScript对象的原型? [英] Why is it frowned upon to modify JavaScript object's prototypes?

查看:124
本文介绍了为什么不赞成修改JavaScript对象的原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里和那里发现了一些关于如何修改JavaScript对象原型的不满意的评论?我个人不知道这可能是一个什么问题。例如,扩展Array对象以获得map和include方法或创建更强大的Date方法?

I've come across a few comments here and there about how it's frowned upon to modify a JavaScript object's prototype? I personally don't see how it could be a problem. For instance extending the Array object to have map and include methods or to create more robust Date methods?

推荐答案

问题在于原型可以在几个地方修改。例如,一个库会将map方法添加到Array的原型中,并且您自己的代码将添加相同的但具有其他目的。因此,一个实现将被打破。

The problem is that prototype can be modified in several places. For example one library will add map method to Array's prototype and your own code will add the same but with another purpose. So one implementation will be broken.

这篇关于为什么不赞成修改JavaScript对象的原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆