Functions Within Objects

V

Vic Sowers

If I do this:

function MyObject() {
this.MyFunc = function () {alert("MyFunc called");}
}

obj1 = new MyObject();
obj2 = new MyObject();

do I get two copies of the code inside MyFunc?
 
M

Michael Winter

function MyObject() {
this.MyFunc = function () {alert("MyFunc called");}
}

obj1 = new MyObject();
obj2 = new MyObject();

do I get two copies of the code inside MyFunc?

Yes. Each time the MyObject constructor is called, the inner function
expression will be evaluated anew, creating separate function objects.
This is why 'prototyped' methods are preferable when feasible.

Mike
 
M

Martin Honnen

Vic said:
If I do this:

function MyObject() {
this.MyFunc = function () {alert("MyFunc called");}
}

obj1 = new MyObject();
obj2 = new MyObject();

do I get two copies of the code inside MyFunc?

ECMAScript allows for some optimization but neglecting that each time
you do new MyObject() indeed each created "instance" gets its own
function object as this.myFunc.

But you should simply code
function MyObject () {

}
MyObject.prototype.MyFunc = function () {alert("MyFunc called");
that way all "instances" share that single function object but due to
the prototype chain set up you can then use e.g.
var obj1 = new MyObject();
obj1.MyFunc();
 
I

Ian Osgood

Martin said:
ECMAScript allows for some optimization but neglecting that each time
you do new MyObject() indeed each created "instance" gets its own
function object as this.myFunc.

But you should simply code
function MyObject () {

}
MyObject.prototype.MyFunc = function () {alert("MyFunc called");
that way all "instances" share that single function object but due to
the prototype chain set up you can then use e.g.
var obj1 = new MyObject();
obj1.MyFunc();

I have a question about this. If you're adding lots of methods to a
prototype, is there a significant difference between these two
constructs?

MyObject.prototype.method1 = function () { ... }
MyObject.prototype.method2 = function () { ... }

MyObject.prototype = {
method1: function () { ... },
method2: function () { ... }
}

Thanks in advance,
Ian
 
V

Vic Sowers

Martin Honnen said:
ECMAScript allows for some optimization but neglecting that each time you
do new MyObject() indeed each created "instance" gets its own function
object as this.myFunc.

But you should simply code
function MyObject () {

}
MyObject.prototype.MyFunc = function () {alert("MyFunc called");
that way all "instances" share that single function object but due to the
prototype chain set up you can then use e.g.
var obj1 = new MyObject();
obj1.MyFunc();

OK, what I really want is:

function MyObject(arg) {
var private = arg;
this.public = 0;
MyFunc = function () {return private*this.public);
}

obj1 = new MyObject(2);
obj1.public = 3
alert(obj1.MyFunc());
obj2 = new MyObject(3);
obj1.public = 3
alert(obj1.MyFunc());

Any suggestions?
 
M

Martin Honnen

Ian Osgood wrote:

If you're adding lots of methods to a
prototype, is there a significant difference between these two
constructs?

MyObject.prototype.method1 = function () { ... }
MyObject.prototype.method2 = function () { ... }

MyObject.prototype = {
method1: function () { ... },
method2: function () { ... }
}

Yes, the first approach with each assignment adds a function property (a
method) to the existing prototype object while the second approach
replaces the existing prototype object with a new object which has two
function properties.
So any assigment using the second approach complete destroys any
previous assignments.
 
M

Martin Honnen

Vic Sowers wrote:

OK, what I really want is:

function MyObject(arg) {
var private = arg;
this.public = 0;
MyFunc = function () {return private*this.public);
I guess you want
this.MyFunc = ..

If you want to have something like private members in JavaScript 1.x
then the suggested way to achieve that is using an inner function and
exploiting closures as above but you need then to be aware that indeed
any creation of new MyObject() creates its own inner function object so
that approach is much more memory intensive as using public members and
share methods as function properties of the prototype. It is up to you
to decide how much objects you need te create and whether private
members are improving your code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top