Hello all, after I spent two days trying to find something like "Math.sign()" or similar in core JavaScript, I decided that quickly writing it myself would probably be smarter (in terms of time management at least ;-) Now, this is really pretty straightforward:> Number.prototype.sign = function() { > if(this<0){return -1} > else if(this>0){return 1} > else {return 0}; > };But I somehow still can''t believe that this is neither in JS nor in Prototype implemented... Now, did I just miss sth., or is it so rarely needed that nobody ever missed it - or do you think it should be added to Prototype? /sascha PS: Oddly, the "Math" class doesn''t seem to like ''prototyping''. - well, "Number" is the better choice anyway... --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
To me this seems like no win. What do you need that for? On Feb 2, 2008 5:20 PM, Sascha Leib <sascha.leib-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello all, > > after I spent two days trying to find something like "Math.sign()" or > similar in core JavaScript, I decided that quickly writing it myself > would probably be smarter (in terms of time management at least ;-) > > Now, this is really pretty straightforward: > > > Number.prototype.sign = function() { > > if(this<0){return -1} > > else if(this>0){return 1} > > else {return 0}; > > }; > > But I somehow still can''t believe that this is neither in JS nor in > Prototype implemented... > > Now, did I just miss sth., or is it so rarely needed that nobody ever > missed it - or do you think it should be added to Prototype? > > /sascha > > PS: Oddly, the "Math" class doesn''t seem to like ''prototyping''. - > well, "Number" is the better choice anyway... > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Math doesn''t like prototyping because its a "library" of static methods, always invoked as such "Math.floor(num)". You never see "myMath = new Math()" so there is your prototyping issue. 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) 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. The implementation could perhaps be simplified if you went with a simpler method. Such as "isPositive" or "isNegative", even that seems a bit much but.. good luck! On Feb 2, 10:22 am, "Deniz Adrian" <deniz.adr...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> To me this seems like no win. What do you need that for? > > On Feb 2, 2008 5:20 PM, Sascha Leib <sascha.l...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hello all, > > > after I spent two days trying to find something like "Math.sign()" or > > similar in core JavaScript, I decided that quickly writing it myself > > would probably be smarter (in terms of time management at least ;-) > > > Now, this is really pretty straightforward: > > > > Number.prototype.sign = function() { > > > if(this<0){return -1} > > > else if(this>0){return 1} > > > else {return 0}; > > > }; > > > But I somehow still can''t believe that this is neither in JS nor in > > Prototype implemented... > > > Now, did I just miss sth., or is it so rarely needed that nobody ever > > missed it - or do you think it should be added to Prototype? > > > /sascha > > > PS: Oddly, the "Math" class doesn''t seem to like ''prototyping''. - > > well, "Number" is the better choice anyway...--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 5, 10:25 am, Matt Foster <mattfoste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Rob, did you get confused?> > 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.I know, thats why I said you never see it. You spent quite some time agreeing with me in the context of an argument. On Feb 5, 6:44 am, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> On Feb 5, 10:25 am, Matt Foster <mattfoste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 6, 7:32 am, Matt Foster <mattfoste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Rob, did you get confused?I don''t think so.> > > 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. > > I know, thats why I said you never see it.Then say so, explicitly. It can only add confusion to introduce "your prototype issue" when the explanation does not need any mention of the prototype object. And I do see it, right there. :-)> You spent quite some time agreeing with me in the context of an > argument.I''d argue that it was a discussion, not an argument. Yes it is. No it''s not... -- 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---