On Feb 5, 10:25 am, Matt Foster <[EMAIL PROTECTED]> wrote:
> Math doesn't like prototyping because its a "library" of static
> methods,
Most native javascript objects are functions that can be used as
constructors, however the Math object is a plain javascript Object, it
does not have a [[construct]] property so it can't be used as a
constructor, nor does it have an internal [[call]] property so it
isn't a function. It's prototype is Object.prototype.
> always invoked as such "Math.floor(num)".
Some of its properties are functions and that is usually the best way
to use them, though they can be used in other ways (see below).
> You never see
> "myMath = new Math()" so there is your prototyping issue.
Because that is a syntax error - the Math object is not a constructor,
it can't be used with new the operator.
> Also this approach seems very odd to me, such a simple inequality
> seems absurd. Given your example, and granted you properly integrated
> this with the number class, how much does the implementation benefit?
>
> if(num.sign() == -1) vs if (num < 0)
I would expect sign to be a property of a Number object, not a method,
and to return + or -, not a randomly chosen value, so:
if (num.sign == '-') ...
> if(num.sign() == 0) vs if (num == 0)
>
> if(num.sign() == 1) vs if (num > 0)
>
> Seeing this laid out as such kind of illustrates my point.
Yes, it does and highlights why returning -1 or 1 doesn't make sense,
consider:
var sign = (num >= 0)? '+' : '-';
or
Number.prototype.sign = function(){
return (this < 0)? '-' : '+';
}
To see if there is a point in extending Number with properties of
Math, consider things like:
var x = Math.floor(y/z);
vs
Number.prototype.floor = function(){
return Math.floor(this);
}
var x = (y/z).floor();
It saves 2 characters per call vs an extra 60 or so to define the
method. It might be faster in some browsers and slower in others.
--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---