Methord in Class

B

binnyva

Hello Everyone,

How can I add a methord to a class? Consider the code...

------------

function makeMovie(name,lead)
{
this.title = name
this.actors = lead
function showLead()
{
alert("Lead Actors are "+this.actors)
}
}

var film = new makeMovie("Whatever","Whoever")
film.showLead()

-----------

This gives an error
"Object don't support this property or methord."
Thanking You in advance,
Binny V A
http://www.geocities.com/binnyva
 
M

Martin Honnen

How can I add a methord to a class? Consider the code...
function makeMovie(name,lead)
{
this.title = name
this.actors = lead
function showLead()
{
alert("Lead Actors are "+this.actors)
}
}

Use
makeMovie.prototype.showLead = function () {
alert("Lead Actors are " + this.actors);
};
 
D

Douglas Crockford

How can I add a methord to a class? Consider the code...
------------

function makeMovie(name,lead)
{
this.title = name
this.actors = lead
function showLead()
{
alert("Lead Actors are "+this.actors)
}
}

var film = new makeMovie("Whatever","Whoever")
film.showLead()

You have two choices:

As you wrote it, showLoad is a private method. This is how you can turn
showLead into a public method:

function makeMovie(name, lead) {
this.title = name;
this.actors = lead;
}
makeMovie.prototype.showLead = function () {
alert("Lead Actors are " + this.actors)
};

This is how you can turn it into a priviledged method:

function makeMovie(name, lead) {
this.title = name;
this.showLead = function () {
alert("Lead Actors are " + lead)
}
}

Notice that lead is a private member. See
http://www.crockford.com/javascript/private.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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top