By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,688 Members | 1,738 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,688 IT Pros & Developers. It's quick & easy.

Functions Within Objects

P: n/a
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?
Aug 12 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 12/08/2005 14:52, Vic Sowers wrote:
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

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Aug 12 '05 #2

P: n/a


Vic Sowers wrote:
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();
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 12 '05 #3

P: n/a

Martin Honnen wrote:
Vic Sowers wrote:
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();
--

Martin Honnen
http://JavaScript.FAQTs.com/


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

Aug 12 '05 #4

P: n/a

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:42**********************@newsread2.arcor-online.net...


Vic Sowers wrote:
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();


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?
Aug 13 '05 #5

P: n/a


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 13 '05 #6

P: n/a


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.