gpt4 book ai didi

JavaScript:是否允许覆盖标准内置对象?

转载 作者:行者123 更新时间:2023-12-01 02:53:36 24 4
gpt4 key购买 nike

例如,我想用我自己的对象替换标准的 DateMath 对象。

我知道这是不明智的,并且会导致一些微妙的问题,例如 instanceoftoString.call 的问题,让我们假设替换者的所有已知公共(public)行为都被模仿。标准允许这样的行为吗?

最佳答案

Does the standard permit such an action?

是的。您可以覆盖 native 对象(如控制台、日期等)的方法,但不能 unpreventable对于没有像数学这样原型(prototype)的人。

例如,

Date.prototype.getDate = function(){ return 5 }; //overriding getDate method

new Date().getDate() //returns 5

关于JavaScript:是否允许覆盖标准内置对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46847554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com