473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

JavaScript constructor syntacs

VK
I never managed to ask before:

Why this backward way is so often used for constructors:

myConstructor = function() { /*empty placeholder*/ }
// and then:
myConstructor.prototype.methodOne = function() {...}
myConstructor.prototype.methodTwo = function() {...}

instead of right away:

function myConstructor() {
this.methodOne = function() {...}
this.methodTwo = function() {...}
}

Is it just a tradition or is it a workaround of some problem I'm not
aware of?

Nov 23 '05 #1
8 1377

VK wrote:
I never managed to ask before:

Why this backward way is so often used for constructors:

myConstructor = function() { /*empty placeholder*/ }
// and then:
myConstructor.prototype.methodOne = function() {...}
myConstructor.prototype.methodTwo = function() {...}

instead of right away:

function myConstructor() {
this.methodOne = function() {...}
this.methodTwo = function() {...}
}

Is it just a tradition or is it a workaround of some problem I'm not
aware of?


The simple difference between the two is memory efficiency.

Using prototype creates each function only once, when the constructor
is created.

Including the function within the constructor, means that a new copy of
the function is created in memory each time you call the constructor to
create an instance.

Of course that is not to say that the second method is in any way
invalid, and indeed the second method is needed if you want to create
public functions which are unique to each instance of your object.

Julian

Nov 23 '05 #2
Julian Turner wrote:
VK wrote:
Why this backward way is so often used for constructors:
It is not a backward way.
myConstructor = function() { /*empty placeholder*/ }
I prefer

function MyConstructor(...)
{
// ...
}
// and then:
myConstructor.prototype.methodOne = function() {...}
myConstructor.prototype.methodTwo = function() {...}
Or using a method to add properties to the prototype.
instead of right away:

function myConstructor() {
this.methodOne = function() {...}
this.methodTwo = function() {...}
}

Is it just a tradition or is it a workaround of some problem
I'm not aware of?

Neither one.
The simple difference between the two is memory efficiency.
Not only that.
Using prototype creates each function only once,
Which means that every object created with this constructor and every
object derived from the prototype automatically has this method. If
one is to use the prototype chain for inheritance, only prototype
properties are inherited through it.
when the constructor is created.
You mean _called_. The constructor is "created" only once,
through definition and during initial variable instantiation.
Including the function within the constructor, means that a new copy of
the function is created in memory each time you call the constructor to
create an instance.

Of course that is not to say that the second method is in any way
invalid, and indeed the second method is needed if you want to create
public functions which are unique to each instance of your object.


True.
PointedEars
Nov 23 '05 #3

Thomas 'PointedEars' Lahn wrote:

[snip]
when the constructor is created.


You mean _called_. The constructor is "created" only once,
through definition and during initial variable instantiation.


Sorry, just for my better understanding and education:-

If you mean by _called_, the evaluation of the the assignment
expressions, by which function expressions are evaluated and assigned
to properties of the prototype, after definition and variable
instantiation, then yes. I.e. the point at which the executing code
(be it global or function code) first encounters and evaluates the
expression:-

MyObject.prototype.myMethod=function(){

};

The point being, that it is the evaluation of this expression that
creates the function.

If you mean by _called_, the point at which you use the constructor,
then no. I.e.

var myInstance=new Constructor();

Unless I have misunderstood something (quite likely) at this point the
prototype functions are already in place in memory.

Regards

Julian

Nov 23 '05 #4
VK
Julian,

I suggest do not jump on it: you cannot win in an unmoderated newsgroup
against a professional troll. Just let him go. I just put recently the
filter on P.E. and life got much better (naturally anyone is welcome to
do the same on me).

I would never notice anything but I sew you answering "to the space" :-)

Nov 23 '05 #5
Julian Turner wrote:
Thomas 'PointedEars' Lahn wrote:
[snip]
It was probably not a good idea to remove context here.
[...]
Unless I have misunderstood something (quite likely) at this point the
prototype functions are already in place in memory.


Probably we have a mutual misunderstanding. Say, for example, that the
constructor is the following method:

function MyObject(x)
{
this.foo = function(y)
{
return x;
}
}

then objects created through

var newObject = new MyObject(...);

have a `foo' method that is unique to an MyObject object, regarding that
there is not the same method for other MyObject objects unless the
conditions for creation (here: the value passed to the constructor) are
the same. If that method is modified, other MyObject objects and objects
derived from MyObject are not affected.

If the constructor and prototype are instead defined as

function MyObject(x)
{
}

MyObject.prototype.foo = function(y)
{
return x;
}

then MyObject objects would also have `foo' method, however a) its return
value depends solely on the value of `x' when it is _called_ and b) all
MyObject objects would share this method since it is accessed through the
prototype chain. Modifying that method would mean that it is modified for
all MyObject objects as well as for all objects derived from MyObject,
unless overwritten there.

That prototype method is not created when the constructor is called (with
`new') but right after the latter was instantiated (provided there is no
code between that forces instantiation).

Did you mean that in the first place?
PointedEars
Nov 23 '05 #6
VK wrote:
I suggest do not jump on it: you cannot win in an unmoderated newsgroup
against a professional troll.
Who is the one of us all here posting unprovable (because nonsensical)
assumptions as the Holy Truth and refuses to see the hard evidence that
proves the opposite? And you are calling me a professional troll? YMMD.
If that is so, it is an honor to be called a troll by you.
Just let him go. I just put recently the filter on P.E. and life got
much better (naturally anyone is welcome to do the same on me).


Yeah, all other people are driving on the wrong side, but not you.
If had had time for this, I would pity you.
PointedEars
Nov 23 '05 #7
VK wrote:
I suggest do not jump on it: you cannot win in an unmoderated newsgroup
against a professional troll.
Who is the one of us all here posting unprovable (because nonsensical)
assumptions as the Holy Truth and refuses to see the hard evidence that
proves the opposite? And you are calling me a professional troll? YMMD.
If that is so, it is an honor to be called a troll by you.
Just let him go. I just put recently the filter on P.E. and life got
much better (naturally anyone is welcome to do the same on me).


Yeah, all other people are driving on the wrong side, but not you.
If I had time for this, I would pity you.
PointedEars
Nov 23 '05 #8

Thomas 'PointedEars' Lahn wrote:
Julian Turner wrote:
Thomas 'PointedEars' Lahn wrote:
[snip]
It was probably not a good idea to remove context here.


True.
[...]
Unless I have misunderstood something (quite likely) at this point the
prototype functions are already in place in memory.


Probably we have a mutual misunderstanding.


I agree. On my side I think.
Say, for example, that the
constructor is the following method:

function MyObject(x)
{
this.foo = function(y)
{
return x;
}
}

then objects created through

var newObject = new MyObject(...);

have a `foo' method that is unique to an MyObject object, regarding that
there is not the same method for other MyObject objects unless the
conditions for creation (here: the value passed to the constructor) are
the same. If that method is modified, other MyObject objects and objects
derived from MyObject are not affected.
Agreed.
If the constructor and prototype are instead defined as

function MyObject(x)
{
}

MyObject.prototype.foo = function(y)
{
return x;
}

then MyObject objects would also have `foo' method, however a) its return
value depends solely on the value of `x' when it is _called_ and b) all
MyObject objects would share this method since it is accessed through the
prototype chain. Modifying that method would mean that it is modified for
all MyObject objects as well as for all objects derived from MyObject,
unless overwritten there.
Agreed.
That prototype method is not created when the constructor is called (with
`new') but right after the latter was instantiated (provided there is no
code between that forces instantiation).

Did you mean that in the first place?


Yes I did. I could have expressed myself better.

Thanks.

Julian

Nov 23 '05 #9

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

Similar topics

0
by: Frank | last post by:
Hey all, I can't seem to get javascript running in my XSL document. <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"...
10
by: JohnS | last post by:
Hi, A lot of functions (classes) in my JavaScript app are singletons. So, I have been exploring ways making JavaScript functions singletons. I thought I'ld run one idea past you all and get some...
11
by: Vincent van Beveren | last post by:
Hi everyone, I have the following code using inheritence The important part is the function getName(). The result should alert 'John Doe': function First(name) { this.name = name; ...
15
by: Encapsulin | last post by:
Hello everybody. I'm trying to change src of quicktime embedded object with javascript: <html><body> <script language="JavaScript"> function Exchange() { document.qtvr.src = "sample2.pano";...
6
by: dpodkuik | last post by:
I have a simple function that does submit for me: <script language="javascript" type="text/javascript"> function sort() { //selected item value from the drop down list var...
7
by: Wm.M.Thompson | last post by:
For a computer programmer JavaScript is not difficult. It is pretty easy to look at some code for the first time and figure out what is going on. This is especially true if you have gratuated...
7
by: andrewfsears | last post by:
I have a question: I was wondering if it is possible to simulate the multiple constructors, like in Java (yes, I know that the languages are completely different)? Let's say that I have a class...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
2
by: Peter Michaux | last post by:
Douglas Crockford doesn't seem to like JavaScript's built-in syntax for building new objects based on a prototype object. The constructor function, its prototype property and the "new" keyword all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.