扩展数字等级 [英] Extend the number class

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

问题描述

我想扩展数字类以具有实例函数,例如奇数甚至所以我可以做一些事情像这样:

I want to extend the number class to have instance functions such as odd and even so I can do something like this:

2.odd() => false
2.even() => true
1.even() => false
1.odd()  => true

扩展类是一种很好的Ruby实践: Ruby检查偶数,浮点数

Extending classes is a good Ruby practise: "Ruby check if even number, float".

是否相同在JavaScript中是真的,还是会导致性能问题或其他一些问题?

Is the same true in JavaScript, or does it cause performance issues or some other problem?

无论如何,尽管我付出了最大的努力,但我无法延伸:

Anyway, I can't extend despite my best efforts:

var NumberInstanceExtensions = {
    accuracy: function(){
        return 'This is cool ' + this
    }
}
$.extend(Number.prototype,NumberInstanceExtensions);

alert( $.type(5) );      //-> number
//alert( 5.accuracy() ); //-> Uncaught SyntaxError: Unexpected token ILLEGAL 

http://jsfiddle.net/VLPTb/2/

我怎样才能让它工作?语法错误让我觉得这不是JavaScript在基础层面上的工作方式。我最好的选择是扩展Math类并改为:

How can I get this to work? The syntax error makes me think this isn't how JavaScript works on a fundamental level. Is my best bet extending the Math class and doing this instead:

Math.odd(2)  => false
Math.even(2) => true
Math.even(1) => false
Math.odd(1)  => true

这似乎比 2.odd()

推荐答案

我想只要你了解你的扩展的副作用,那么你就是好的。我经常修改String原型来添加一个elipsis方法,所以我可以做一些事情,比如

I think as long as you understand the side-effects of your "extension" then you're okay. I often modify the String prototype to add an "elipsis" method so I can do things like

"SomeString".elipsis()

但从头开始。你不是在JavaScript中扩展类。 JavaScript是一种基于原型的语言。您可以修改原型以执行您需要的操作。

But start at the beginning. You're not "extending classes" in JavaScript. JavaScript is a prototype-based language. You can modify prototypes to do what you need.

您将无法直接向数字本身添加方法。但是,您可以修改Number对象的原型:

You won't be able to add a method directly to the number itself. You can, however modify the prototype of the Number object:

Number.prototype.even = function(){    
    return this.valueOf() % 2 === 0;
}

这样,您将无法使用以下语法:

With this, you won't be able to use the following syntax:

10.even();

但是,既然你不是硬编码的东西,否则你不需要这个功能,您可以执行以下操作:

But, since you aren't hard-coding stuff, otherwise you wouldn't need this function anyways, you CAN do the following:

var a = 10;
a.even(); //true

我可能会说你可以考虑添加一个实用程序对象来做这些事情,因为修改原始原型并不总是保证无副作用。

I might say that you could consider adding a utilities object to do these things, because modifying primitive prototypes is not always guaranteed to be side-effect free.

此功能并不能为您带来任何好处。你正在检查奇数和偶数,用另一行替换一行代码。想想差异:

This function does not really provide any gain for you. You're checking for odd and even, replacing one line of code with another. Think about the difference:

var a = 10;
var aIsEven = a.even();

vs:

var a = 10;
var aIsEven = a % 2 === 0;

您获得三个字符的代码,第二个选项不太可能破坏您的JavaScript。

You gain three characters of code, and the second option is less likely to break your "JavaScript".

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

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