472,959 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,959 software developers and data experts.

new String( "foo" ) VS. "foo"

Why does "foo" instanceof String return false?

It just seems counterintuitive to me.

I mean, sure, I can always check the constructor:

var fooVar = "foo";

var isString = fooVar.constructor == String;

var isNotStringObject = fooVar instanceof String;

It seems counterintuitive to me that an object is not an instance of
it's constructor.

Garrett

Jul 12 '07 #1
1 2239
<dh**********@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
Why does "foo" instanceof String return false?
String literals result in string primitive values and primitive values
are not objects.
It just seems counterintuitive to me.
Are you thinking of javascript as being in some way related to Java?
I mean, sure, I can always check the constructor:

var fooVar = "foo";

var isString = fooVar.constructor == String;
And let the type-converting side effects of the dot operator fool you
into thinking that - fooVar - might be an object?

Dot operators need their left-had side arguments to be objects so they
call the internal - ToObject - function with the retrieved value of that
operand as an argument. The internal - ToObject - function returns a
String object when its argument is a string primitive value and so with,
for example, the expression - "foo".indexOf('s') - the - "foo". - part
effectively evaluates to a string object equivalent to - new
String("foo") -, and it is this (internal and intermediate) object on
which the - indexOf - method is called.
var isNotStringObject = fooVar instanceof String;
Here the left operand for - instanceof - is the primitive value (no type
conversion is implied by - instanceof -) and as primitive values are not
objects the - String - constructor's - [[HasInstance]] - method will
return false in step 1 of its algorithm.
It seems counterintuitive to me that an object is not an
instance of it's constructor.
But makes perfect sense when you know that it is not an object at all.

Richard.

Jul 12 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Carroll | last post by:
I'm at my wits end with this silly thing. I'm new to python, so please excuse my impending ignorance ;). I am trying to execute a command, and get the output back into a string variable. Here's...
68
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I...
3
by: TR | last post by:
At runtime, I'd like to instantiate an object (e.g. myInvoice) when all I know is the name of the class that has to instantiated (e.g. "Invoice"). Dim DesiredClass as String = "Invoice" '// now...
2
by: Manuel | last post by:
In example, here: ------------------------- namespace Gooey Button::Button(Window* aParent, const std::string& aText, BasicButtonGroup* aGroup) : BasicButton(aParent, aText, aGroup) { }...
3
by: key9 | last post by:
Hi all I am confuse of how to override operate "<<" Sample , I've got 2 class class Terminal { public:
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
8
by: Mark | last post by:
Could someone provide me with details or a link on how the line of code executes underneath the hood? Assume it's executed in an ASP.NET application. string blah =...
5
by: Eric Lilja | last post by:
Is there something "elegant" in the standard library I can use to perform a "shifted insert" in a std::string? Let me examplify what I mean with shifted insert. Say I have: std::string foo =...
1
by: Alexnb | last post by:
Basically I want the code to be able to pick out how many strings there are and then do something with each, or the number. When I say string I mean how many "strings" are in the string "string...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.