[jQuery] Re: A question for John Resig

2009-02-19 Thread RobG



On Feb 18, 2:53 pm, pbcomm pbc...@gmail.com wrote:
 This might be a stupid question, but I have to ask ...
 What is the reason for not having functions like isString, isNumber,
 isBoolean, etc? Is it just because that would create extra function
 calls?

The simple reason for me is that typeof tells you the type, it doesn't
tell you for certain whether the tested variable is callable or not.

 Native objects that implement [[call]] must return 'function',
however host objects can return any value they like, so:

  if (typeof foo != 'function')

doesn't mean foo isn't callable unless you *know* it is a native
object.

The same goes for all the other is(whatever) functions.  The use of
instanceof is not a suitable alternative in a general sense as it
doesn't work across frames.


--
Rob


[jQuery] Re: A question for John Resig

2009-02-19 Thread Michael Geary

I'm curious what the benefit of that would be. Given that the
window.undefined property exists and has the undefined value, I'd think they
would give the same result.

-Mike

 From: John Resig
 
 ...we'll probably switch from foo === undefined
 to typeof foo === undefined - we'll see



[jQuery] Re: A question for John Resig

2009-02-19 Thread John Resig

 I'm curious what the benefit of that would be. Given that the
 window.undefined property exists and has the undefined value, I'd think they
 would give the same result.

They give the same results for properties, at least:

  someObject.undefinedProperty === undefined

but not for variables that are undefined (meaning that 'var
someUndefinedVar' or 'window.someUndefinedVar' was not used):

  someUndefinedVar === undefined // error
  typeof someUndefinedVar === undefiend // works

I think it'd be better to be consistent everywhere.

--John


[jQuery] Re: A question for John Resig

2009-02-19 Thread tres

Thanks for clearing that up John.

-Trey



On Feb 19, 10:07 am, John Resig jere...@gmail.com wrote:
  Why implement jQuery.isFunction when you can also just go typeof
  variable == 'function'?

 You can see some of the cases that we handle that normal typeof can't, 
 here:http://dev.jquery.com/browser/trunk/jquery/test/unit/core.js#L176

 --John


[jQuery] Re: A question for John Resig

2009-02-18 Thread John Resig

Extra function calls sure - plus there's really no need for those
methods, they're already a part of JavaScript.

typeof FOO === string
typeof FOO === number
typeof FOO === boolean

--John



On Tue, Feb 17, 2009 at 11:53 PM, pbcomm pbc...@gmail.com wrote:

 This might be a stupid question, but I have to ask ...
 What is the reason for not having functions like isString, isNumber,
 isBoolean, etc? Is it just because that would create extra function
 calls?



[jQuery] Re: A question for John Resig

2009-02-18 Thread pbcomm

I know they are there and I've used added functions to save on the
code size, which makes a big difference when used a lot. That's why I
wanted to see if there are some other drawbacks I was not aware of.

On Feb 18, 7:57 am, John Resig jere...@gmail.com wrote:
 Extra function calls sure - plus there's really no need for those
 methods, they're already a part of JavaScript.

 typeof FOO === string
 typeof FOO === number
 typeof FOO === boolean

 --John

 On Tue, Feb 17, 2009 at 11:53 PM, pbcomm pbc...@gmail.com wrote:

  This might be a stupid question, but I have to ask ...
  What is the reason for not having functions like isString, isNumber,
  isBoolean, etc? Is it just because that would create extra function
  calls?


[jQuery] Re: A question for John Resig

2009-02-18 Thread Aleem B

 I know they are there and I've used added functions to save on the
 code size, which makes a big difference when used a lot.

It doesn't make any difference save for a few characters.


[jQuery] Re: A question for John Resig

2009-02-18 Thread Michael Lawson

I don't know about Javascript, but a good deal depends on how that
functionality is implemented, and what language you're in.  They're not
always the same thing, and shouldn't always be used like they are the same
thing.  Again though, they may be in Javascript, i'm not saying they're
not. (I'm not really a javascript expert)

In the Java language, for example, you have the instanceof operator.  This
is fine if you only need to check the type of something only a few times,
but if it's a several times per unit time kinda thing, you start to run
into efficiency problems.  So using methods that make use of more efficient
algorithms become the better way to go.

cheers

Michael Lawson
Content Tools Developer, Global Solutions, ibm.com
Phone:  1-919-517-1568 Tieline:  255-1568
E-mail:  mjlaw...@us.ibm.com

'Examine my teachings critically, as a gold assayer would test gold. If you
find they make sense, conform to your experience, and don't harm yourself
or others, only then should you accept them.'


   
  From:   Aleem B ale...@gmail.com   
   
  To: jquery-en@googlegroups.com   
   
  Date:   02/18/2009 08:36 AM  
   
  Subject:[jQuery] Re: A question for John Resig   
   






 I know they are there and I've used added functions to save on the
 code size, which makes a big difference when used a lot.

It doesn't make any difference save for a few characters.

inline: graycol.gifinline: ecblank.gif

[jQuery] Re: A question for John Resig

2009-02-18 Thread Kean

Matt,

While it would not affect the results, I believe you can shave a few
ms off by using  ===

On Feb 18, 11:26 am, Matt Kruse m...@thekrusefamily.com wrote:
 On Feb 18, 6:57 am, John Resig jere...@gmail.com wrote:

  typeof FOO === string
  typeof FOO === number
  typeof FOO === boolean

 == is sufficient. typeof always returns a string.

 Matt Kruse


[jQuery] Re: A question for John Resig

2009-02-18 Thread Matt Kruse

On Feb 18, 2:20 pm, Kean shenan...@gmail.com wrote:
 While it would not affect the results, I believe you can shave a few
 ms off by using  ===

Over 10,000,000 iterations, I see no difference in time between using
== and ===.
If the return type of 'typeof' varied, a difference might be found.

It's just a minor quibble anyway. It's similar to lines like this in
the jquery source:

if ( typeof text !== object  text != null )

Fixing things like this would tighten and improve the code a bit, IMO.

Matt Kruse


[jQuery] Re: A question for John Resig

2009-02-18 Thread Kean

Seems like my hunch is incorrect, thanks for correcting.

On Feb 18, 12:43 pm, Matt Kruse m...@thekrusefamily.com wrote:
 On Feb 18, 2:20 pm, Kean shenan...@gmail.com wrote:

  While it would not affect the results, I believe you can shave a few
  ms off by using  ===

 Over 10,000,000 iterations, I see no difference in time between using
 == and ===.
 If the return type of 'typeof' varied, a difference might be found.

 It's just a minor quibble anyway. It's similar to lines like this in
 the jquery source:

 if ( typeof text !== object  text != null )

 Fixing things like this would tighten and improve the code a bit, IMO.

 Matt Kruse


[jQuery] Re: A question for John Resig

2009-02-18 Thread tres

Why implement jQuery.isFunction when you can also just go typeof
variable == 'function'?

-T



On Feb 19, 7:54 am, Kean shenan...@gmail.com wrote:
 Seems like my hunch is incorrect, thanks for correcting.

 On Feb 18, 12:43 pm, Matt Kruse m...@thekrusefamily.com wrote:

  On Feb 18, 2:20 pm, Kean shenan...@gmail.com wrote:

   While it would not affect the results, I believe you can shave a few
   ms off by using  ===

  Over 10,000,000 iterations, I see no difference in time between using
  == and ===.
  If the return type of 'typeof' varied, a difference might be found.

  It's just a minor quibble anyway. It's similar to lines like this in
  the jquery source:

  if ( typeof text !== object  text != null )

  Fixing things like this would tighten and improve the code a bit, IMO.

  Matt Kruse


[jQuery] Re: A question for John Resig

2009-02-18 Thread John Resig

It really depends in which browser you test. I'm seeing little to no
difference in IE and Firefox - but a noticeable difference in Safari
and Opera.

http://dev.jquery.com/~john/ticket/equals/

Safari: +3ms, +2ms, +7ms, +3ms, +4ms
Opera: +46ms, +35ms, +21ms, +19ms, +36ms

(over 500,000 iterations)

It's not massive, but it's measurable.

As far is its use - I see no problem with it. It's more precise and
instills good habits (being more precise about what you want to
target).

I also see no problem with this:
if ( typeof text !== object  text != null )

We want items that aren't objects, aren't null, and aren't undefined.
Looks fine to me.

As far as convention, we're already working to formalize it:
http://docs.jquery.com/JQuery_Core_Style_Guidelines

It's very basic right now (and we'll probably switch from foo ===
undefined to typeof foo === undefined - we'll see) but it's a work
in progress.

--John



On Wed, Feb 18, 2009 at 3:43 PM, Matt Kruse m...@thekrusefamily.com wrote:

 On Feb 18, 2:20 pm, Kean shenan...@gmail.com wrote:
 While it would not affect the results, I believe you can shave a few
 ms off by using  ===

 Over 10,000,000 iterations, I see no difference in time between using
 == and ===.
 If the return type of 'typeof' varied, a difference might be found.

 It's just a minor quibble anyway. It's similar to lines like this in
 the jquery source:

 if ( typeof text !== object  text != null )

 Fixing things like this would tighten and improve the code a bit, IMO.

 Matt Kruse



[jQuery] Re: A question for John Resig

2009-02-18 Thread John Resig

 Why implement jQuery.isFunction when you can also just go typeof
 variable == 'function'?

You can see some of the cases that we handle that normal typeof can't, here:
http://dev.jquery.com/browser/trunk/jquery/test/unit/core.js#L176

--John


[jQuery] Re: A question for John Resig

2009-02-18 Thread John Resig

 Safari: +3ms, +2ms, +7ms, +3ms, +4ms
 Opera: +46ms, +35ms, +21ms, +19ms, +36ms

I forgot to mention that this means that == is about 10% slower than
=== in both browsers.

--John


[jQuery] Re: A question for John Resig

2009-02-18 Thread pbcomm

lol, i can see this kind of took a different route ;) thank you all
for replying ;)

On Feb 18, 6:25 pm, John Resig jere...@gmail.com wrote:
  Safari: +3ms, +2ms, +7ms, +3ms, +4ms
  Opera: +46ms, +35ms, +21ms, +19ms, +36ms

 I forgot to mention that this means that == is about 10% slower than
 === in both browsers.

 --John