Closed
Bug 15566
Opened 26 years ago
Closed 26 years ago
Unable to create constructor functions on objects.
Categories
(Core :: JavaScript Engine, defect, P3)
Tracking
()
VERIFIED
INVALID
People
(Reporter: tedk, Assigned: mike+mozilla)
Details
The JS engine issues a syntax error on a constructor function this is a member
of an object. For example, function obj.Cls() {}, would issue the error (object
was instantiated as obj = new Object() ). The code below clearly demonstrates
the problem on Mozilla M9. Note the code works just fine with MS JScript.
Reading over ECMA262 the below code _should_ work but the spec isnt clear in
this area.
<html>
<head>
<script>
var namespace = new Object(); // declare a namespace object
</script>
<script>
//
// Declare a Shape object as a member of the namespace
// Note this is just regular old javascript object usage.
// IE 4+ works as expected.
// NN all break. Mozilla M9 breaks.
//
function namespace.Shape(_color) { this.m_color = _color; }
function namespace.Shape.prototype.show() { alert(this.m_color); }
</script>
<script>
var o = new namespace.Shape("red"); // instantiate object
o.show();
o = null;
</script>
</head>
<body>
</body>
</html>
my reading is that instances of Objects do not implement the [[Construct]]
property, so using the new operator on such an object should result in an error
(11.2.2). waldemar, mike, is that right?
example of the syntax error
js> function namespace.Shape( c ) { this.color = c; }
27: missing ( before formal parameters:
27: function namespace.Shape( c ) { this.color = c; }
27: ..................^
js>
| Assignee | ||
Comment 4•26 years ago
|
||
Ted, you've found an area where the ECMA standard isn't very closely specified,
and the netscape and microsoft implementations of JavaScript differ. (Waldemar,
please check me on this; this behavior is *not* specified, right?)
You should be able to get what you want in a cross-browser way by saying
namespace.Shape = function(_color) { this.m_color = _color }
that is - create an anonymous function and explicitly assign it as a property of
your namespace, rather than depending on function declaration syntax knowing how
to navigate a string of object.property.dot.references to find the right place
to bind the new function.
Property assignment within the function declaration is an interesting syntax,
though; maybe we'll want to look at supporting it in the future. Is Microsoft
or some other reference encouraging this idiom?
Updated•26 years ago
|
Status: NEW → RESOLVED
Closed: 26 years ago
Resolution: --- → INVALID
Comment 5•26 years ago
|
||
The ECMA standard is unambiguous here -- the code should produce a syntax error.
A function declaration allows only a single identifier between the 'function'
keyword and the opening parenthesis. See chapter 13 of ECMA.
It looks like Microsoft has made some kind of an extension of the language in
JScript. I don't know the details about what they did or why, but it is
definitely non-ECMA.
A future ECMA standard will have better syntax for declaring classes and methods.
However, that syntax will be different from the proposed "function id1.id2(...)".
Implementing "function id1.id2(...)" is problematic because of order-of-
evaluation issues -- according to ECMA, function declarations must be processed
before any other code is run. At that time id1's value doesn't exist yet, so the
method can't be created.
Waldemar
Comment 6•25 years ago
|
||
Owner/reporter, dare to mark this 'verified', if applicable?
You need to log in
before you can comment on or make changes to this bug.
Description
•