4

I'm still learning JavaScript,reading books,utilizing FireBug,experimenting.

I'm amazed and stuck on thing below.

Have function declaration:

    var t = function (args){
            ...
    }

It's assumed,that it's varargs.

I call it like:

<body onload="t({to:100,from:0})">

It's possible to get from argument value by calling:

args.from

The result of typeof args.from is number
That looks sane.
pay attention: number is in lower case

I am interested of what instance args.from is.
Actually, can't get its instance value.
tried:

args.from instanceof Number
args.from instanceof String
args.from instanceof Object
args.from instanceof Boolean

It is not Number - very strange
It's not Object - quite strange
It's not String -that's OK
It 's not Boolean -that's OK
It's neither null nor 'undefined' - looks OK.

What is it?

4
  • BTW, that's not varargs; it's an ordinary parameter that happens to hold an object. Commented Dec 23, 2011 at 14:31
  • yes,I know, i wrote,that it's "assumed" varargs, opposite to Java,where it should be definitely declared Commented Dec 23, 2011 at 14:33
  • A value of the primitive Number type is not "an instance of" anything. The instanceof operator is used for Object values, not primitive values... Commented Dec 23, 2011 at 14:42
  • ... so, use typeof for primitive values, and instanceof for Object values... Commented Dec 23, 2011 at 14:52

2 Answers 2

5

That's a primitive numeric value.
It isn't an instance of any class.

instanceof can only return true on objects (for which typeof returns "object" or "function").

This has nothing to do with the args object; you can get the same falsity from 4 instanceof Number.

By contrast, new Number(4) is an object wrapping the primitive 4, so typeof new Number(4) === "object and new Number(4) instanceof Number === true

Sign up to request clarification or add additional context in comments.

3 Comments

oh,yes, i just realized,number lower case points on this also
A takeaway point from this is that you should not be doing strict type checking with instanceof everywhere a la Java. That's simply not the style of programming to which Javascript is suited. Use duck typing and document expected arguments and interfaces.
@FrancisAvila- +1 for pointing out that strict typing in javascript is unreliable (and probably impossible in a general sense). Better to just test for the feature(s) needed or expected and go from there.
2

As passed, from.to is a primitive type, so you can't call instanceof on it.

If you really want to use instanceof instead of typeof, wrap it in an Object:

var from = Object(args.from);
alert(from instanceof Number);   // true!

10 Comments

interesting thing: typeof results object in lowercase after wrapping.SLaks already explained it)
@sergionni yes, that's expected.
@Alnitak Your answer, in its current form, suggests an anti-pattern - wrapping primitive values in objects and then applying instanceof on them. The correct approach would be to not use the instanceof operator for primitive values, but to use typeof instead. I'm pointing this out, because in the future, some inexperienced programmer might see this answer and come to a false conclusion...
@ŠimeVidas text modified slightly. One question though - what if you might also be expecting non-primitive values? Wouldn't that be a case for converting to Object and then using instanceof ?
@ŠimeVidas now I'm understand,that it's primitive and I understand,that sometimes I need some Object properties to deal with,so need wrapping
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.