Calling internal 'public' methods within js objects

B

bizt

Hi,

Below I have a simple object / function thing (still getting head
round these) declaration:

function MyObject() {
this.alertMe = function() {
alert('hello');
};
this.alertMeAgain() {
this.alertMe(); // wrong syntax
};
}

Notice how in the method - alertMeAgain - I have attempted to call the
other public function, alertMe(). Now this doesnt work as I have come
to know - how do I call internal methods from another methods with the
same object??

Also, I pretty new to using custom objects in js. Is this the most
common method of building objects in js? I come from using PHP which
uses a quite similar means of declaring classes so I like this one.
Cheers

Burnsy
 
V

VK

Hi,

Below I have a simple object / function thing (still getting head
round these) declaration:

function MyObject() {
this.alertMe = function() {
alert('hello');
};
this.alertMeAgain() {
this.alertMe(); // wrong syntax
};

}

Notice how in the method - alertMeAgain - I have attempted to call the
other public function, alertMe(). Now this doesnt work as I have come
to know - how do I call internal methods from another methods with the
same object??

Also, I pretty new to using custom objects in js. Is this the most
common method of building objects in js? I come from using PHP which
uses a quite similar means of declaring classes so I like this one.

Such methods creates closures on each call - it is hugely ineffective
unless your methods must to preserve states between calls (static in
VB sense). In such case make an instance reference holder like:

function MyObject() {
var $ = this; // holder
var foo = 'bar'; // protected var
this.alertMe = function() {
alert(foo);
};
this.alertMeAgain = function() {
$.alertMe();
};
}

var obj = new MyObject;
obj.alertMeAgain(); // alerts "bar"
 
G

Gregor Kofler

bizt meinte:
Hi,

Below I have a simple object / function thing (still getting head
round these) declaration:

function MyObject() {
this.alertMe = function() {
alert('hello');
};
this.alertMeAgain() {
this.alertMe(); // wrong syntax
};
}

Did you mean something like this.alertMeAgain = function() { }?

Otherwise you're trying to call the undefined MyObject::alertMeAgain and
the semicolon behind that statement is missing.

Gregor
 
B

bizt

Such methods creates closures on each call - it is hugely ineffective
unless your methods must to preserve states between calls (static in
VB sense).

Basically i prefer to use something where my vars are encapsulated as
i need to have individual instances of some in each object (i have
ajax requests in my real ones using xmlhtmlrequest objects and may be
sending requests simultaniously - this has given problem if i use the
same object in quick succession). I would first think to use objects
in other languages but dont have much experiece using javascript
objects - this is my only knowledge.

Anyway, when you speak of creating closures and preserve states - what
do you mean? Sorry, again new to this. I assume an instance reference
wont create a seperate instance of my object. is this correct? want to
keep memory usage low as possible. thanks

burnsy
 
V

VK

Basically i prefer to use something where my vars are encapsulated as
i need to have individual instances of some in each object (i have
ajax requests in my real ones using xmlhtmlrequest objects and may be
sending requests simultaniously - this has given problem if i use the
same object in quick succession). I would first think to use objects
in other languages but dont have much experiece using javascript
objects - this is my only knowledge.

Anyway, when you speak of creating closures and preserve states - what
do you mean?

Look at the sample again:

function MyObject() {
var $ = this; // holder
var foo = 'bar'; // protected var
this.alertMe = function() {
alert(foo);
};
this.alertMeAgain = function() {
$.alertMe();
};
}

the new object instance goes into constructor, gets its properties and
leaves: but because of inner functions assigned to alertMe and
alertMeAgain - the whole current context of MyObject is preserved for
this particular instance, so you can address $ and foo from instance
method any time later. It is like as if each instance would take take
with it its own clone copy of the constructor. If you know Perl then
it is very close to its sub closures:
sub outer {
my $x = $_[0];
local *inner = sub {return $x + 2};
return 3 + inner();
}
The difference is that in Javascript you can nest functions directly
as well:
function outer() {
function inner() {
}
}
Sorry, again new to this. I assume an instance reference
wont create a seperate instance of my object. is this correct?

If you mean like
var a = new MyObject;
var b = a;
then now, of course b=a doesn't clone an instance, it remains only one
pointed by both a and b.
 
R

RobG

Hi,

Below I have a simple object / function thing (still getting head
round these) declaration:

In javascript, functions *are* objects.
function MyObject() {

Identifiers starting with a capital letter are usually reserved for
constructors, i.e. functions that you intend to call with the new
operator. Is that what you are doing here?

How you call a function determines the value of its this keyword, so
I'll assume MyObject is a constructor and that you are calling it
something like:

var newObject = new MyObject();

this.alertMe = function() {
alert('hello');
};

If MyObject is called as a constructor, its this keyword is set to a
reference to a new object. The above line assigns an anonymous
function to an alertMe property of that object.

this.alertMeAgain() {
this.alertMe(); // wrong syntax
};

Here you attempt to call the alertMeAgain property of the newly
created object, but you haven't added that property or given it a
value so of course it gives an error.

}

Notice how in the method - alertMeAgain - I have attempted to call the
other public function, alertMe().

An alertMeAgain property was not added to the object, nor was an
attempt made to assign a function to it.

Now this doesnt work as I have come
to know - how do I call internal methods from another methods with the
same object??

Firstly you have to declare the or initialise the methods, then assign
them to properties of an appropriate object. You can then call them.
Further advice may be possible if you show a bit more code so we can
tell if you are trying to use a constructor or plain object.

Also, I pretty new to using custom objects in js. Is this the most
common method of building objects in js? I come from using PHP which
uses a quite similar means of declaring classes so I like this one.

There are a variety of ways to build objects with properties that are
similar to classes in other languages. I think it is best to learn
the standard prototype iheritance method, then go from there. There
are a number of patterns for emulating public and private methods in
javascript, which one you choose is up to you. e.g.

<URL: http://www.litotes.demon.co.uk/js_info/private_static.html >
 

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
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top