473,386 Members | 1,652 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,386 software developers and data experts.

Functions Within Objects

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
6 1450
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


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

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

"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


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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: vrillusions | last post by:
I've been using functions since I first started using php, but I've been hearing more and more about classes, which I never really looked at that much. I understand the whole OO programming...
12
by: bissatch | last post by:
Hi, Generally if I re-use code, I use a function. If I need to use these functions over a number of pages I write the function to an include file where all pages have access. So when should I...
5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
9
by: relaxedrob | last post by:
Howdy All! I am still getting my head around a few base concepts. Any comments or criticisms on the below definitions would be most welcome! A function is an object. JavaScript objects have...
1
by: optimistx | last post by:
How to build or find an object browser for javascript? E.g. Delphi integrated developement environment ('ide') offers a very practical object browser for Pascal language. When typing a name with...
11
by: tshad | last post by:
I am setting up some of my functions in a class called MyFunctions. I am not clear as to the best time to set a function as Shared and when not to. For example, I have the following bit...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
4
by: =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire | last post by:
Hello all, I'm learning C and I still am struggling to understand some basic concepts. For example, I read in the standard that with functions such as strcpy, 'If copying takes place between...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.