473,946 Members | 30,497 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 3172
VK wrote:
Richard Cornford wrote:
>Who said Opera was the only browser reporting the syntax error?

Do you have any other one besides Opera 9?
Yes. Not reporting this syntax error as a syntax error appears to be the
exception (unsurprisingly ).
I have no idea about Safari,
but I would like to dismiss it right away
Which brings us back to what I said earlier. You write code without
recognising a syntax error in the language you are using and then when
your scripts only work in a couple of browsers at most you blame the
browsers. That the fault here is yours rather than the browser's is
obvious as soon as you realise that people exist who do manage to write
effective code for Safari. You think they are doing that by putting
disproportionat e effort into the task, but it is more likely that they
are just presenting Safari with code devoid of syntax errors, mystical
incantations and your other nonsense, and the result just works.
(in case if): if you take a dring every time this
UA does something right with
scripting, you'll end up thirsty and angry :-)
The VK definition of what is "right" is too detached from reality for
that to be a criteria for judging anything.
Any other mainstream browser?
Earlier today it was "Any ECMAScript-compliant engine erroring out on the
below, please", but now that it should be obvious that
ECMAScript-compliant browsers are likely to generate syntax errors with
code that contains syntax errors you want to retreat to your 'safe'
ground of "mainstream browsers". Which presumably is "mainstream
browsers" as defined by VK, and so pretty much Windows IE and Firefox
only.
A good hint: if you're in dubts about something, check it
on Netscape 4.7
Idiot.
In 99% of cases whatever it does is what was really
intended,
Unlikely as Netscape 4.7 implemented ECMA 262 2nd Edition (with
extensions) and your "mainstream browsers" (and other current scriptable
browsers) implement ECMA 262, 3rd Edition.
if it contradicts to ECMAScript specs it simply means
that the free-lancers
Have you looked at the list of the people who wrote ECMA 262?
failed to describe properly what they've seen.
With the contributors to ECMA 262 having direct access to the source code
for both JavaScript(tm) and JScript it is unlikely that observation
determined their behaviour.
One more time: there is not syntax error here,
Only when "here" is restricted to the contents of your deranged mind.
but there is Opera's parser laziness:
It is not lazy to report a syntax error when one is seen.
instead of further interprete the source and to
understand that this is an expression w/o assignment:
There is nothing in the language's syntax that allows such an
interpretation, and you cannot assume that any browser that does not
report the syntax error is making such an interpretation. As far as you
know they are just recognising the futility of the construct and just
filtering it out. Indeed experimentation suggests that the syntax error
is being filtered out rather than being interpreted as a Function
Expression. Try:-

<html>
<head>
<title></title>
<script type="text/javascript">
var a;

function(){aler t('I was recognised 1');}

(a);

var b = function(){aler t('I was recognised 2');}

(a);

</script>
</head>
<body>
</body>
</html>

- in Firefox and IE 6 and you see "I was recognised 2" alerted by the
second function expression but no sign of 'I was recognised 1', which
would be the consequences of the - function(){aler t('I was recognised
1');} - line being interpreted as a function expression, as can be
illustrated by wrapping that line in parenthesise to give:-

<html>
<head>
<title></title>
<script type="text/javascript">
var a;

(function(){ale rt('I was recognised 1');})

(a);

var b = function(){aler t('I was recognised 2');}

(a);

</script>
</head>
<body>
</body>
</html>

- where the absence of any syntax error allows both 'I was recognised 1'
and 'I was recognised 2' to be alerted.

So " interprete the source and to understand that this is an expression
w/o assignment" is not what Firefox or IE are doing with the syntax
error. Instead they appear to be silently ignoring it. And silently
ignoring syntax errors is less useful than reporting them.
instead of that
it just errors out on the first opportunity.
Which tells the programmer at the earliest point possible that they have
made an error.

Obviously you would not appreciate that, as you don't like being told
that you have made an error, and prefer not to recognise them when you
have. But then that is one of the things makes you by far the worst
programmer I have ever encountered.

Richard.
Nov 8 '06 #31
In article <11************ **********@m73g 2000cwd.googleg roups.com>, VK
<sc**********@y ahoo.comwrites
>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.
You obviously don't know what a parser does. It's a pattern matcher. A
match tells the compiler which syntax rule the code matches. Then the
compiler knows exactly what the programmer wants the program to do.

For instance, if the parser is given
var x = 42;
the parser finds that the code matches the syntax rule for creating a
variable named x with the initial value 42.

On the other hand, if the parser is given
x var 42 =;
the parser doesn't find a match with any of the hundreds of syntax rules
so the compiler doesn't know what the programmer was thinking.

In VK's universe the browser's AI plugin would come to life at this
point and say that it believes the programmer meant ... well, what?

>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.
VK is confused once again. The parser's job is to see which syntax rule
the code matches. It's a style guide or lint program that's expected to
say it's dangerous to use 'with'. Thus the parser must declare that
; ++i; --i; void function() {};
are all legal since each matches a syntax rule. A style guide might say
that i++ is preferred to ++i, or that void is rarely needed, but it
doesn't have to say that.

Does javascript have syntax rules? Yes : they're written in ECMA 262.
Does every javascript programmer have to be able to read them? No, but
if they can't they mustn't tell people what the rules are or aren't.

John

Postscript :
No syntax rule matches
; function() {};
but there is one that matches
; void function() {};
Strange, but true.
--
John Harris
Nov 10 '06 #32

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
9975
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
11552
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
11325
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,...
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
7404
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
6097
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...
0
6318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4928
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3526
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.