473,763 Members | 4,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Why does "foo" instanceof String return false?

It just seems counterintuitiv e to me.

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

var fooVar = "foo";

var isString = fooVar.construc tor == String;

var isNotStringObje ct = fooVar instanceof String;

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

Garrett

Jul 12 '07 #1
1 2295
<dh**********@g mail.comwrote in message
news:11******** **************@ k79g2000hse.goo glegroups.com.. .
Why does "foo" instanceof String return false?
String literals result in string primitive values and primitive values
are not objects.
It just seems counterintuitiv e 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.construc tor == 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 isNotStringObje ct = 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 counterintuitiv e 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
7189
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 what I think should work from the docs I've read. The system I need to get this working on is 2000 professional, I'm posting this from my linux box, so please don't look poorly upon me for the windows thing, it wasn't my choice. import commands...
68
4365
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 don't like the brackets.
3
1092
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 create an instance of the desired class ??? Can this be done? I hope the question is clear.
2
1853
by: Manuel | last post by:
In example, here: ------------------------- namespace Gooey Button::Button(Window* aParent, const std::string& aText, BasicButtonGroup* aGroup) : BasicButton(aParent, aText, aGroup) { } Window& Button::draw()
3
2159
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
6508
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" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
8
2649
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 = ConfigurationSettings.AppSettings Once your application has called for the "foo" key once, how expensive is to call the same line again? My gut tells me that the CLR likely stores these values in memory in a string dictionary or similar. Looking up the value requires the...
5
1929
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 = "abc"; std::string shifted_foo = shifted_insert(foo, 'd'); shifted_foo should after the shifted_insert() equal "dab".
1
165
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 string string non-string string" Does that help? Fredrik Lundh wrote: --
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.