473,945 Members | 25,823 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 3170
VK wrote:
Did you try?
<script type="text/javascript">
function() {}
</script>

That is a perfectly valid syntax: but useless program-wise.
FunctionDeclara tion is not evaluated, so there is no "hook" left to use
this function in your program. This is all what they tried (but failed)
to say.
JSLint reports:

Missing name in function statement.

http://www.JSLint.com/
Nov 7 '06 #11
VK
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 ;-)

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. Functionally it is the same as
<script type="text/javascript">
4;
</script>
or
<script type="text/javascript">
"foobar";
</script>

Nov 8 '06 #12
VK wrote:
Douglas Crockford wrote:
>VK wrote:
>>Did you try?
<script type="text/javascript">
function() {}
</script>

That is a perfectly valid syntax:
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 ;-)

But I have to reconsider my previous statement:
It was a false statement.
<script type="text/javascript">
function() {}
</script>
is not a FunctionDeclara tion:
Could not be a FunctionDeclara tion because it has no Identifier and the
Identifier is not optional in a FunctionDeclara tion.
it is still FunctionExpress ion.
Being a function expression does not make it "perfectly valid syntax".
Expressions are not the units of a javascript program; they are
FunctionDeclara tions and Statements. We have ruled out
FunctionDeclara tions and the only form of statements that can be
constructed from a single Expression are ExpressionState ments, where an
Expression statement is _explicitly_ forbidden from commencing with the
- function - keyword. Thus an ExpressionState ment consisting of only a
Function Expression is not "perfectly valid syntax".

I told you this yesterday, but instead of verifying that I was correct
here you are trying to defend another of your false statements. If you
don't know what constitutes syntactically correct javascript (and you
have made it completely clear over the years that you don't, and are
not capable of learning it) it would be better to keep quiet on the
subject.
Here we have "expression without assignment" wich is totally valid
It is a syntax error. It was a syntax error yesterday and will still be
a syntax error tomorrow.
byt rather useless. Functionally it is the same as
<script type="text/javascript">
4;
</script>
or
<script type="text/javascript">
"foobar";
</script>
Except that neither of those are syntax errors, and so may appear in
javascript source code without risking it not being interpretable.

Richard.

Nov 8 '06 #13
VK

Richard Cornford wrote:
It is a syntax error. It was a syntax error yesterday and will still be
a syntax error tomorrow.
byt rather useless. Functionally it is the same as
<script type="text/javascript">
4;
</script>
or
<script type="text/javascript">
"foobar";
</script>

Except that neither of those are syntax errors, and so may appear in
javascript source code without risking it not being interpretable.
Any ECMAScript-compliant engine erroring out on the below, please.

<html>
<head>
<title>Untitl ed Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<script type="text/javascript">
try {
eval(function() {});
}
catch(e) {
window.alert(e. message);
}

</script>
</head>

<body>

</body>
</html>

Nov 8 '06 #14
VK wrote:
Richard Cornford wrote:
>VK wrote:
>>>VK wrote:
Did you try?
<script type="text/javascript">
function() {}
</script>
That is a perfectly valid syntax:
<snip>
>>Here we have "expression without assignment" wich is totally valid
<snip>
>It is a syntax error. It was a syntax error yesterday and will still be
a syntax error tomorrow.
<snip>
Any ECMAScript-compliant engine erroring out on the below, please.
You are a halfwit. In what sense does the code below relate to anything
that has come before?
<html>
<head>
<title>Untitl ed Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<script type="text/javascript">
try {
eval(function() {});
<snip>

A FunctionExpress ion is a completely valid argument to a function call,
and the - eval - function does not do anything with non-string
arguments but return them. There is no syntax error here, and so its
not erorring is unsurprising.

It is telling of your actual understanding of this subject that you
make false statements and then when you are corrected start posting
code that you are incapable of seeing as having no relevance to the
question in hand. I suppose here we are suffering again form you not
actually knowing what the code you write is supposed to do, and so
thinking this to relevant when it is not.

Richard.

Nov 8 '06 #15
VK

Richard Cornford wrote:
It is a syntax error. It was a syntax error yesterday and will still be
a syntax error tomorrow.
byt rather useless. Functionally it is the same as
<script type="text/javascript">
4;
</script>
or
<script type="text/javascript">
"foobar";
</script>

Except that neither of those are syntax errors, and so may appear in
javascript source code without risking it not being interpretable.
Any ECMAScript-compliant engine erroring out on the below, please.

<html>
<head>
<title>Untitl ed Document</title>
<script type="text/javascript">
function(){}
</script>
</head>
<body>
</body>
</html>

P.S. eval in the previous sample was used to wrap possible syntax
errors so make them catchable at run-time. It is useless in this case
(because there are no syntax errors to expect), just an automated
doing: "error", "syntax" keywords triggered the corresponding style in
my head :-)

Nov 8 '06 #16
VK wrote:
Richard Cornford wrote:
>VK wrote:
>>>VK wrote:
Did you try?
<script type="text/javascript">
function() {}
</script>
That is a perfectly valid syntax:
<snip>
>>Here we have "expression without assignment" wich is totally valid
<snip>
>It is a syntax error. It was a syntax error yesterday and will still be
a syntax error tomorrow.
<snip>
>It is a syntax error. It was a syntax error yesterday and will still be
a syntax error tomorrow.
<snip>
Any ECMAScript-compliant engine erroring out on the below, please.
You mean like Opera 9 reporting a syntax error?
<html>
<head>
<title>Untitl ed Document</title>
<script type="text/javascript">
function(){}
</script>
</head>
<body>
</body>
</html>

P.S. eval in the previous sample was used to wrap possible syntax
errors
Whatever you may have thought you were using it for the - eval -
function only executes code if its argument is a string, so passing it
a function object guaranteed that it did nothing.
so make them catchable at run-time.
And that is what would have happened, if the argument had been a
string.
It is useless in this case
(because there are no syntax errors to expect),
Not true, If the argument had been the string "function() {};" then the
syntax error would have happened. But achieving that pre-supposes some
sort of knowledge of how javascript works, so you just posted nonsense
instead.
just an automated doing: "error", "syntax" keywords triggered the
corresponding style in my head :-)
Gibberish.

Richard.

Nov 8 '06 #17
VK

Richard Cornford wrote:
VK wrote:
Richard Cornford wrote:
VK wrote:
VK wrote:
Did you try?
<script type="text/javascript">
function() {}
</script>
That is a perfectly valid syntax:
<snip>
>Here we have "expression without assignment" wich is totally valid
<snip>
It is a syntax error. It was a syntax error yesterday and will still be
a syntax error tomorrow.
<snip>
It is a syntax error. It was a syntax error yesterday and will still be
a syntax error tomorrow.
<snip>
Any ECMAScript-compliant engine erroring out on the below, please.

You mean like Opera 9 reporting a syntax error?
Yep... Opera 9 errors out. That proves again the old "Web axiom" that
for any feature will be at least one UA that will get it wrong.

Lucky this "feature" has only theoretical interest.

Nov 8 '06 #18
VK wrote:
Richard Cornford wrote:
<snip>
>>Any ECMAScript-compliant engine erroring out on the below, please.

You mean like Opera 9 reporting a syntax error?

Yep... Opera 9 errors out. That proves again the old "Web axiom" that
for any feature will be at least one UA that will get it wrong.
In what sense is reporting a syntax error when code contains a syntax
error getting it wrong? It might be better argued that browsers
tolerating what should be syntax errors are 'getting it wrong', though
browsers being tolerant in the face of incompetent web developers is
hardly a new phenomenon.

Your "web axiom" is typically worthless. A much more useful, and
applicable axiom is; "be liberal in what you accept and strict in what
you send". with being liberal in what they accept being what some
browsers do in tolerating a syntax errors in javascript source code,
and being strict in what you send meaning that you write syntactically
correct code to be sent to the browser in the first place. After all,
has anyone observed any browser ever having a problem with receiving
syntactically correct javascript source code?
Lucky this "feature" has only theoretical interest.
Maybe only "theoretica l interest" for you, as you will never know
enough to distinguish between correct syntax and incorrect. But while
you treat browsers that do not understand the 'code' you write as not
being supportable others write code that works fine with them (even
genuinely cross-browser). Could it be that the browsers you deprecate
so much are actually just not tolerating the code you are sending them
because not knowing what is syntactically correct in javascript you are
sending them syntax errors that you cannot recognise. Wouldn't that
leave knowing syntactic correctness having a very practical impact on
the ability to surpass your 'skills' and so write genuinely
cross-browser code?

Richard.

Nov 8 '06 #19
VK

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. 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.

The exact parsing algorithm was given by you in this thread, as well
as two possible branches of this algorithm.

<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. If Opera 9
then go to step 4, otherwise go to step 5
4) Opera 9: "booh, booh... bad syntax... :-("
4) Others: function expression w/o assignment, seems useless but not
our damn business.

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.

Nov 8 '06 #20

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
12654
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
10149
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...
1
11319
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
9872
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...
1
8239
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
6093
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
6315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4520
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3523
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.