473,946 Members | 8,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An object's constructor name as a string. Can definitely be determined?

Hi,

I want to know the name of an object's constructor function as a
string. Something like this

<script type="text/javascript">
function Foo(){};
var a = new Foo();
alert('"' +
a.constructor.t oString().match (/function\s*([a-zA-Z\$][\w\$]+)[^\w\$]/)[1]
+ '"');
</script>

Although this works in IE, Opera, Firefox, Safari I'm not sure that it
is guaranteed to work.

I looked in the ECMA-262 specs about the returned string for
Function.protot ype.toString() in section 15.2.4.2. It says that the
whitespace, line terminators and semi-colon use are implementation
dependent. I think the above regular expression can handle those
difference.

What I'm worried about is that he specs say the return value has the
form of a "function declaration". Does that mean the returned string
will definitely contain the name of the constructor function? Could it
instead return something like just "function () {}"?

Thank you,
Peter

Nov 6 '06
31 3171
VK wrote:
Douglas Crockford wrote:
>>Did you try?
<script type="text/javascript">
function() {}
</script>
>JSLint reports:

Missing name in function statement.

http://www.JSLint.com/

JSLint is not the script engine, so it's allowed to have different
opinions on different topics ;-)
That is true, but it is irrelevant. If you ran all of your snippets thru JSLint
before posting to this forum, your reputation would be significantly less tarnished.
But I have to reconsider my previous statement:
<script type="text/javascript">
function() {}
</script>
is not a FunctionDeclara tion: it is still FunctionExpress ion. Here we
have "expression without assignment" wich is totally valid byt rather
useless.
It is a syntax error. Your example isn't just useless. It is wrong.
Nov 8 '06 #21
VK wrote:
Richard Cornford wrote:
>"be liberal in what you accept and strict in what you send".

Exactly: and this rule (which is especially true for javascript
engines) is violated on Opera 9.
It is a principle, not a rule. That said, it's not one that I would want
to apply to a programming or scripting language.
The rule is that a javascript program may error out only if no
available interpretations left: this is why even utterly wrong
scripts are "working" sometimes - but often not in the intended way.
Which is precisely why I'd prefer it not happen with programming
languages. Better to have a program (or better yet, a compiler, where
applicable) raise an exception and stop executing than guessing and
acting unpredictably.

[snip]

Mike
Nov 8 '06 #22
VK said the following on 11/8/2006 9:03 AM:
Richard Cornford wrote:
>"be liberal in what you accept and strict in what
you send".

Exactly: and this rule (which is especially true for javascript
engines) is violated on Opera 9.
You really are an idiot.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 8 '06 #23
VK wrote:
Richard Cornford wrote:
>"be liberal in what you accept and strict in what
you send".

Exactly: and this rule (which is especially true for javascript
engines) is violated on Opera 9.
It all goes straight over your head, doesn't it? You have complete
control over the code you write, but you would rather blame a web
browser for doing no more than it claims to do than learn how to take
control of the code you write.
The rule is that a javascript program
may error out only if no available interpretations left: this is why
even utterly wrong scripts are "working" sometimes -
Finally a subject where you have some experience.
but often not in the intended way.

The exact parsing algorithm was given by you in this thread,
No it was not.
as well as two possible branches of this algorithm.
I certainly mentioned that the only two applicable target constructs
were either Statements or Function Declarations.
<script type="text/javascript">
function(){}
</script>

1) Found "function" keyword left hand side, searching for identifier.
There is no sense in "left hand side". the keyword would be the next
token in a sequence where all preceding tokens (if any) were accounted
for.
2) If an identifier found then this is FunctionDeclara tion;
It is not a FunctionDeclara tion unless it fully satisfies the
production rules for FunctionDeclara tion.
if not then go to step 3
3) No identifier found, left hand side anonymous function.
Anonymous functions don't have sides. And is Function Declaration is
excluded the target construct is now Statement.
If Opera 9
then go to step 4, otherwise go to step 5
4) Opera 9: "booh, booh... bad syntax... :-("
Or, you now know you have messed up and can do something about fixing
your code.

(Also, you are assuming that Opera 9 is the only browser that will
apply the syntax rules in accordance with the specification. It almost
certainly is not, and indeed it would be more rational to assume that
all browsers would apply the language's syntax rules and then be
surprised at the few that are tolerant of this type of error).
4) Others: function expression w/o assignment, seems useless
but not our damn business.
And now you have someone who thinks that what they have done is
correct, and will probably be going on to repeat their mistake on the
grounds that it "works", even though it is then no more than a mystical
incantation repeated because it has been used in code that "works" in
the past and has not directly caused problems (at least under
superficial testing).

I don't need to tell you anything about programming by mystical
incantation, or superficial testing, as you are certainly the expert in
those areas.
This way Opera 9 between i)trying to further interpret the code and ii)
error out is choosing to error out, which is not OK for a reliable
script engine.
It is that sort of thinking that results in the code you write, which
is anything but reliable.

Richard.

Nov 8 '06 #24
VK
Hah! The entire Co in one spot (though the posting order hierarchy is
not correct IMHO. Richard?)

OK, so much easy to answer:

<html>
<head>
<title>Untitl ed Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">

function(){} + 2 == 'Learn JavaScript better, guys';

// And do not take single erroneus implementation
// for some standard.
</script>
</head>
<body>
</body>
</html>

P.S. Damn German beer is good! I'm getting sad just thinking of coming
back to Bud-water in a few weeks. :-(
:-)

Nov 8 '06 #25
VK wrote:
<snip>
OK, so much easy to answer:
<snip>
function(){} + 2 == 'Learn JavaScript better, guys';
Another syntax error and another pointless operation (if it were not a
syntax error). So what is your point?
// And do not take single erroneus implementation
// for some standard.
<snip>

Who said Opera was the only browser reporting the syntax error? Your
question was whether there were any browsers which had a problem with
your syntax error, and if one can be named that question has been
answered. There is no need for any single browser to be taken as
defining a standard when a standard already exists and is unambiguous
on the subject.

Richard.

Nov 8 '06 #26
VK

Richard Cornford wrote:
Who said Opera was the only browser reporting the syntax error?
Do you have any other one besides Opera 9? I have no idea about Safari,
but I would like to dismiss it right away (in case if): if you take a
dring every time this UA does something right with scripting, you'll
end up thirsty and angry :-)

Any other mainstream browser?

A good hint: if you're in dubts about something, check it on Netscape
4.7 In 99% of cases whatever it does is what was really intended, if it
contradicts to ECMAScript specs it simply means that the free-lancers
failed to describe properly what they've seen.

One more time: there is not syntax error here, but there is Opera's
parser laziness: instead of further interprete the source and to
understand that this is an expression w/o assignment: instead of that
it just errors out on the first opportunity.

Nov 8 '06 #27
Richard Cornford wrote:
VK wrote:
<snip>

Richard, you really are generous. Most would have given up long ago.

Peter

Nov 8 '06 #28
In article <11************ **********@h54g 2000cwb.googleg roups.com>, VK
<sc**********@y ahoo.comwrites

<snip>
><script type="text/javascript">
function(){}
</script>

1) Found "function" keyword left hand side, searching for identifier.
2) If an identifier found then this is FunctionDeclara tion; if not then
go to step 3
3) No identifier found, left hand side anonymous function.
<snip>

Obviously the programmer has forgotten to type something like
this.a =
at the beginning of the line, or has forgotten to type the function name
in the middle of the line. A halfway decent compiler will say
!!!ERROR!!!
so the programmer sees and corrects the mistake straight away.

If the compiler says nothing the programmer has to write to
comp.lang.javas cript saying it doesn't work, then spend hours
complaining that no-one has replied quickly enough, and then complain
again when they ask to see more code because he thinks the error is
somewhere else, which it isn't.

Unfortunately, some people get paid for the time needed to find obscure
errors in their code. They get paid less if the error is made too
obvious, so they scream loudly when the error is highlighted straight
away.

John
--
John Harris
Nov 8 '06 #29
VK
1) Found "function" keyword left hand side, searching for identifier.
2) If an identifier found then this is FunctionDeclara tion; if not then
go to step 3
3) No identifier found, left hand side anonymous function.
<snip>
Obviously the programmer has forgotten to type something like
this.a =
at the beginning of the line, or has forgotten to type the function name
in the middle of the line. A halfway decent compiler will say
!!!ERROR!!!
so the programmer sees and corrects the mistake straight away.
Obviously I will never hire you for an application development :-(
because you have a holy mix of that a parser/engine is and what a
tidy-like suggestive checker is.

The engine's job is not to enforce some "preferred style" of
programming or some "best practice" onto developers. It has much more
humble but much more technically difficult task: from each segment of
the source code try to produce an execution code non-violating given
border criterias. That is not damn parser business what is this code
for and why did one use it this way.
Tidy-like checkers are free to declare ++i or --i harmful or declare
void function(){};
as an error (an opinion shared by no one real engine). Parser has not
such freedom: it has to eat whatever fed until chokes.

Nov 8 '06 #30

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

Similar topics

28
20378
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object doesn't support this property or method." in IE, and nothing happens in Firebird.
2
2995
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the Intel compiler it has stopped working. I think the singleton pattern static instance thing may be at the root of the problem, but I'm not sure. I had to add calls to instance() in regCreateFn, which gets the behaviour more in line with what I was...
1
3989
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of size n. There are n!/(s!(n-s)!) ways to do this. Donald E. Knuth gives several methods (algorithms) to generate all the s-combinations in . In such procedure-oriented way, each s-combination is processed while it's being generated. In some
15
1943
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following two? (1)
16
2921
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList (Collection) of Purchase objects. Now, these Purchase objects were pulled from a datasource Datasource. The...
8
2033
by: a | last post by:
I'm trying to save data from a custom object into the profile object, but it is not structured the way that I want. I'm trying to get the custom object to serialize as xml to a Profile object like so: <Teachers> <Teacher> <Classes> <Class>
0
1847
by: a | last post by:
I need to create an instance of a custom object 'School.Teacher' and use it in a Profile object. I'm developing a bad case of "Pretzel Logic" thinking about this. Filling the custom object 'School.Teacher' as an ArrayList creates the proper information (see aspx code below), but I'm unable to use this ArrayList in the Profile object. The aspx code below shows the attempt and error message.
7
3595
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id - FirstName - LastName - CompanyId (many-to-one )
275
12655
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
9974
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
11549
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11320
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
10679
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8240
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7402
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
6095
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...
2
4523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3524
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.