I am cracking my head over this fragment of javascript code I found in the beloved prototype.js library: Object.extend(String.prototype, { // ... truncate: function(length, truncation) { length = length || 30; truncation = truncation === undefined ? ''...'' : truncation; return this.length > length ? this.slice(0, length - truncation.length) + truncation : String(this); }, // ... }); I understand what the String.truncate function does and how it does it, but I don''t understand why there is a difference in the way the length and truncation parameters get their default values: length = length || 30; truncation = truncation === undefined ? ''...'' : truncation; Why is prototype checking for undefined for the truncation parameter? Why didn''t they write: truncation = truncation || ''...''; And what are those three equal signs doing? Prototype has some funky syntax in there. Most I have been able to figure out, but this difference I cannot explain. Thanks for any help! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
=== is for comparing identical values ex; var myNull = ''null'', (myNull == null) => true (myNull == undefined) => true (myNull === undefined) => false The ''truncatation === undefined'' is being used to check if the truncation parameter is passed to the truncate method. Their reasoning is probably for someone wanting to truncate a string without ellipses. Someone can pass an empty string to truncate() which will return a truncated string without ellipses. ex: ''My String''.truncate(5); => ''My St...'' ''My String''.truncate(5, ''''); => ''My St'' BTW, you can also easily truncate a string without ellipses by using just ''My String''.substring(0, 5) => ''My St'' Hope that helps... ~jake On Sep 16, 9:42 am, OddesE <stijndew...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am cracking my head over this fragment of javascript code I found in > the beloved prototype.js library: > > Object.extend(String.prototype, { > // ... > > truncate: function(length, truncation) { > length = length || 30; > truncation = truncation === undefined ? ''...'' : truncation; > return this.length > length ? > this.slice(0, length - truncation.length) + truncation : > String(this); > }, > > // ... > > }); > > I understand what the String.truncate function does and how it does > it, but I don''t understand why there is a difference in the way the > length and truncation parameters get their default values: > > length = length || 30; > truncation = truncation === undefined ? ''...'' : truncation; > > Why is prototype checking for undefined for the truncation parameter? > Why didn''t they write: > > truncation = truncation || ''...''; > > And what are those three equal signs doing? Prototype has some funky > syntax in there. Most I have been able to figure out, but this > difference I cannot explain. > > Thanks for any help!--~--~---------~--~----~------------~-------~--~----~ 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 16 sep, 16:39, Jake <jrockow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The ''truncatation === undefined'' is being used to check if the > truncation parameter is passed to the truncate method.Ok.. but isn''t length = length || 30; doing the same thing? You can call "My string".truncate(); right? Without any parameters? The || in the expression evaluates the right hand side if length == null. So.. I still don''t really get this.. why should the first parameter not be compared against undefined? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
a || b evaluates to the first "non-falsy" value, which is anything except for 0, "" (empty string), false, null, or undefined. If you checked truncation || "...", and you pass an empty string, then it would still use "...", as the empty string is falsy, thus needing to check explicitly against undefined. Regards, -Nicolas On 9/16/07, OddesE <stijndewitt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 16 sep, 16:39, Jake <jrockow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > The ''truncatation === undefined'' is being used to check if the > > truncation parameter is passed to the truncate method. > > Ok.. but isn''t > > length = length || 30; > > doing the same thing? You can call > > "My string".truncate(); > > right? Without any parameters? The || in the expression evaluates the > right hand side if length == null. > > So.. I still don''t really get this.. why should the first parameter > not be compared against undefined? > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 Sep 17, 9:14 am, "Nicolás Sanguinetti" <godf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> a || b evaluates to the first "non-falsy" value,Not correct: it returns the first non-false value or the last one, it will *always* return one of the operand expressions (in this case, either a or b).> which is anything > except for 0, "" (empty string), false, null, or undefined.Whether a or b evaluates to true or false is based on the toBoolean algorithm in ECMAScript Language Ed 3 specification, section 9.2 which includes NaN (which evaluates to false). Oh, and I hate the term "falsy" :-) The equality operator "==" uses the abstract equality comparison algorithm, where an empty string (''''), boolean false, the number zero, null and undefined all resolve to false. In other words, the type of expression isn''t tested, only whether it is equivalent to true or false (the algorithm for that is a bit long a complicated, but not to much, see Section 11.9.3 of the spec). When the strict equals operator "===" is used, the expressions on either side must also match by type, so: false == 0 // true false === 0 // false -- 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 9/17/07, RobG <rgqld-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> > > On Sep 17, 9:14 am, "Nicolás Sanguinetti" <godf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > a || b evaluates to the first "non-falsy" value, > > Not correct: it returns the first non-false value or the last one, it > will *always* return one of the operand expressions (in this case, > either a or b).I don''t see the difference between what you and I said there :)> > which is anything > > except for 0, "" (empty string), false, null, or undefined. > > Whether a or b evaluates to true or false is based on the toBoolean > algorithm in ECMAScript Language Ed 3 specification, section 9.2 which > includes NaN (which evaluates to false).Heh, missed NaN, I was sure I was missing something.> Oh, and I hate the term "falsy" :-) > > The equality operator "==" uses the abstract equality comparison > algorithm, where an empty string (''''), boolean false, the number zero, > null and undefined all resolve to false. In other words, the type of > expression isn''t tested, only whether it is equivalent to true or > false (the algorithm for that is a bit long a complicated, but not to > much, see Section 11.9.3 of the spec). > > When the strict equals operator "===" is used, the expressions on > either side must also match by type, so: > > false == 0 // true > false === 0 // false > > > -- > 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 9/17/07, RobG <rgqld-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> > > On Sep 17, 9:14 am, "Nicolás Sanguinetti" <godf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > a || b evaluates to the first "non-falsy" value, > > Not correct: it returns the first non-false value or the last one, it > will *always* return one of the operand expressions (in this case, > either a or b).I don''t see the difference between what you and I said there :)> > which is anything > > except for 0, "" (empty string), false, null, or undefined. > > Whether a or b evaluates to true or false is based on the toBoolean > algorithm in ECMAScript Language Ed 3 specification, section 9.2 which > includes NaN (which evaluates to false).Heh, missed NaN, I was sure I was missing something.> Oh, and I hate the term "falsy" :-) > > The equality operator "==" uses the abstract equality comparison > algorithm, where an empty string (''''), boolean false, the number zero, > null and undefined all resolve to false. In other words, the type of > expression isn''t tested, only whether it is equivalent to true or > false (the algorithm for that is a bit long a complicated, but not to > much, see Section 11.9.3 of the spec). > > When the strict equals operator "===" is used, the expressions on > either side must also match by type, so: > > false == 0 // true > false === 0 // false > > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Sorry about the hiccup, gmail died on me when I hit send. -N On 9/17/07, Nicolás Sanguinetti <godfoca-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 9/17/07, RobG <rgqld-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote: > > > > > > On Sep 17, 9:14 am, "Nicolás Sanguinetti" <godf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > a || b evaluates to the first "non-falsy" value, > > > > Not correct: it returns the first non-false value or the last one, it > > will *always* return one of the operand expressions (in this case, > > either a or b). > > I don''t see the difference between what you and I said there :) > > > > which is anything > > > except for 0, "" (empty string), false, null, or undefined. > > > > Whether a or b evaluates to true or false is based on the toBoolean > > algorithm in ECMAScript Language Ed 3 specification, section 9.2 which > > includes NaN (which evaluates to false). > > Heh, missed NaN, I was sure I was missing something. > > > Oh, and I hate the term "falsy" :-) > > > > The equality operator "==" uses the abstract equality comparison > > algorithm, where an empty string (''''), boolean false, the number zero, > > null and undefined all resolve to false. In other words, the type of > > expression isn''t tested, only whether it is equivalent to true or > > false (the algorithm for that is a bit long a complicated, but not to > > much, see Section 11.9.3 of the spec). > > > > When the strict equals operator "===" is used, the expressions on > > either side must also match by type, so: > > > > false == 0 // true > > false === 0 // false > > > > > > -- > > 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 Sep 17, 11:23 pm, "Nicolás Sanguinetti" <godf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9/17/07, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote: > > > > > On Sep 17, 9:14 am, "Nicolás Sanguinetti" <godf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > a || b evaluates to the first "non-falsy" value, > > > Not correct: it returns the first non-false value or the last one, it > > will *always* return one of the operand expressions (in this case, > > either a or b). > > I don''t see the difference between what you and I said there :)Maybe it''s splitting hairs, but if the preceding expressions evaluate to false, then the last will be returned regardless of whether it''s false or not. Saying that it returns the first non-false value leaves open the question of what happens if none of the expressions are non- false. :-) -- 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 Sep 17, 2007, at 1:16 AM, RobG wrote:> Oh, and I hate the term "falsy" :-)... and that''s the truthy! I used to have issues with it too, but I''ve heard it from enough PhDs that I feel comfortable with it. Besides, it''s a useful word! TAG --~--~---------~--~----~------------~-------~--~----~ 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 9/17/07, RobG <rgqld-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> On Sep 17, 11:23 pm, "Nicolás Sanguinetti" <godf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 9/17/07, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote: > > > > > > > > > On Sep 17, 9:14 am, "Nicolás Sanguinetti" <godf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > a || b evaluates to the first "non-falsy" value, > > > > > Not correct: it returns the first non-false value or the last one, it > > > will *always* return one of the operand expressions (in this case, > > > either a or b). > > > > I don''t see the difference between what you and I said there :) > > Maybe it''s splitting hairs, but if the preceding expressions evaluate > to false, then the last will be returned regardless of whether it''s > false or not. > > Saying that it returns the first non-false value leaves open the > question of what happens if none of the expressions are non- > false. :-):) You''re right, I was ambiguous there. On 9/17/07, Tom Gregory <tomg-PGZyUNKar/Q@public.gmane.org> wrote:> > > On Sep 17, 2007, at 1:16 AM, RobG wrote: > > > Oh, and I hate the term "falsy" :-) > > ... and that''s the truthy! > > I used to have issues with it too, but I''ve heard it from enough PhDs > that I feel comfortable with it. Besides, it''s a useful word!Same thoughts here :) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---