整数原型 [英] Integer prototype

查看:114
本文介绍了整数原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在整数上制作原型?

how can you make an prototype on an Integer?

Integer.prototype.num = function(dec){
    return number_format(this.toString(), dec);
}


推荐答案

没有整数在JavaScript中,只需数字。您可以按照您展示的方式扩展数字

There's no Integer in JavaScript, just Number. You can extend Number in the way you've shown.

关于扩展构建,有两个阵营 - 以这种方式的类型。一个阵营说这是邪恶的,你不能这样做;使用您传递对象的单独函数。另一个阵营说这就是为什么我们有原型,所以我们可以扩展我们认为合适的东西的功能。

There are two camps with regard to extending the built-in types in this way. One camp says it's evil and you must not do it; use a separate function you pass the object into. The other camp says this is exactly why we have prototypes, so that we can extend the functionality of things as we see fit.

如果你在第二阵营,有一些陷阱要避免(但请参阅下面的世界正在改变...)

If you're in the second camp, there are pitfalls to avoid (but see "The world is changing..." below):


  • 从不,永远,永远以这种方式扩展 Object.prototype 。你将打破大量的代码。

  • 非常非常谨慎地扩展 Array.prototype ,你会打破一个公平的代码。

  • Never, ever, ever extend Object.prototype in this way. You'll break a huge amount of code.
  • Be very, very wary of extending Array.prototype, you'll break a fair bit of code.

两种情况下的原因是当你以这种方式扩展原型时,你创建了一个原型上的可枚举属性。这意味着它出现在 for..in 循环中。 JavaScript世界中的自定义和练习是必须在空白对象上没有可枚举的属性( {} )。数组可能更容易被接受,但要注意那些不太懂的人什么 for..in 确实会认为它会遍历数组索引;如果以这种方式扩展 Array.prototype ,你将打破它们的循环。 (我认为他们的循环已经被破坏了,但是把它放在一边......)

The reason in both cases is that when you extend a prototype in that way, you create an enumerable property on the prototype. That means it shows up in for..in loops. Custom and practice in the JavaScript world is that there must be no enumerable properties on a blank object ({}). Arrays may be more acceptable, but beware people who don't really understand what for..in does who think it will loop through the array indexes; if you extend Array.prototype in this way, you'll break their loops. (I would argue their loops were already broken, but leave that to the side...)

但是世界正在发生变化,因为 ECMAScript5 为我们提供了一种方法来添加不可枚举的属性对象,以及所有模糊的最新浏览器实现它。它被称为 Object.defineProperty

The world is changing, though, because ECMAScript5 gave us a way to add non-enumerable properties to objects, and all vaguely up-to-date browsers implement it. It's called Object.defineProperty:

Object.defineProperty(Number.prototype, "num", {
    enumerable: false,
    value: function() { ... }
});

请注意 enumerable:false false enumerable 的默认值;我在此明确强调)。这意味着它不会出现在 for..in 循环中,因此可以软化该参数以防止扩展原生对象的原型。

Note that enumerable: false (false is the default for enumerable; I'm being explicit here for emphasis). That means it doesn't show up on for..in loops, and so softens that argument against extending prototypes of native objects.

然而,即使使用 Object.defineProperty ,命名冲突的可能性(其他代码也扩展原型,或未来版本的JavaScript中添加的功能)仍然存在。 。

However, the possibility of naming conflicts (with other code also extending the prototype, or features added in future versions of JavaScript) remains, even with Object.defineProperty.

这篇关于整数原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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