473,498 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using new foo where foo is a new Foo object.

Why isn't this allowed?

function Foo(){
this.param1 = 'Constructor Function Foo';
}

var foo = new Foo;
var newfoo = new foo;

The last line above gives an error. I thought all objects could be
used as constructors. But it seems the foo object above cannot be used
with the 'new' operator to create more objects. Does anyone know why
that is?
Thanks.
Dec 21 '07 #1
2 1312
On Dec 22, 2:18 am, blackholebutter...@gmail.com wrote:
Why isn't this allowed?

function Foo(){
this.param1 = 'Constructor Function Foo';

}

var foo = new Foo;
var newfoo = new foo;

The last line above gives an error. I thought all objects could be
used as constructors. But it seems the foo object above cannot be used
with the 'new' operator to create more objects. Does anyone know why
that is?
Thanks.
Javascript's object inheritence model is prototypal. But for a bunch
of historical reasons the designers of the language decided to
implement a model which tries to emulate classical inheritence (hence
the new keyword). This is why as you've found out "new" works for
"Constructor" objects but not for regular objects.

Other prototypal languages have an "object" operator instead of "new".
Traditionally, the difference between "object" and "new" is that while
"new" creates an object instance from a class (or constructor
functions in javascript) "object" creates an object instance directly
from a live object. Unfortunately the designers of javascript, while
trying to bend it to emulate classical inheritence, neglected to
provide an object operator/function.

Fortunately javascript's prototype model is complete enough that we
can write our own object method:

function object(o) {
function F() {}
F.prototype = o;
return new F();
}

So now you can write:

function Foo(){
this.param1 = 'Constructor Function Foo';
}

var foo = new Foo;
var newfoo = object(foo);

See http://javascript.crockford.com for more on this topic.
Dec 22 '07 #2
You thought wrong. Constructors have to be objects that implement the
internal [[Construct]] method which applies to native Function objects and
certain host objects. See the ECMAScript Specification, Edition 3 Final,
sections 11.2.2 and 13.2.
Section 13.2 mentions a [[Class]] property, and elsewhere it says it
is "a string value indicating the kind of this object." Does that mean
that if I use the typeof operator only objects of type "Function" can
be used as constructors? How to tell if a given object implements the
internal [[Construct]] method?
Dec 23 '07 #3

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

Similar topics

4
2045
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
0
6678
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
28
20270
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()',...
11
2166
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
17
4169
by: Danny J. Lesandrini | last post by:
The following code works with a standard MDB to navigate to a particluar record (with a DAO recordset, of course) but it's giving me problems in an ADP I'm working on. Dim rs As ADODB.Recordset...
9
10857
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent...
4
3387
by: Chris | last post by:
Hi, everything works apart from the last line :-(( rng.Value2.ToString() An exception is thrown : "Old format or invalid type library" It gets compiled though (so he recognizes the property...
0
6412
by: Michelle Keys | last post by:
I am trying to call a print function to print a string from a database using javascript. Which is RC_DATA of Varchar2(2500). This is a javascript is not being used. I have a thing that needs to...
14
3124
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
2
3476
by: Ryan | last post by:
Hi, I receive an access denied error (see below) when attempting to send an email with BodyFormat=MailFormat.Html from an asp.net page. Exactly the same code works fine in a console...
0
7125
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,...
0
7002
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...
0
7165
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,...
0
7379
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...
0
4588
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...
0
3093
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...
0
1417
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 ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
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...

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.