Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

oo javascript question 2

Status
Not open for further replies.

richardko

Programmer
Jun 20, 2006
127
US
hi,
I am new to oo in javascript so i am not sure if i am asking the right question.
I have a script below and its not working for some reason (and its late right now)...but could anyone point out why i am getting the error:
Error: object.changeData is not a function

<html>
<head>
<script type="text/javascript">
function myClass(ini)
{
myData=ini;
this.showdata = displaydata;

function displaydata()
{
alert(myData);
}
function changeData(newData)
{
myData= newData;
}
}

function changeState(object)
{
object.changeData('300');
object.showdata();

}
</script>
</head>
<body>
<script type="text/javascript">

var obj=new Array();
obj[0]=new myClass('0');
obj[1]=new myClass('11');
obj[2]=new myClass('22');


</script>
<input type="button" name="button" onClick="changeState(obj[0])" value="Change State">
</body>
</html>
 
[tt]function myClass(ini)
{
[green]var[/green] myData=ini; [green]//var for better encap[/green]
this.showdata = displaydata;
[blue]this.changeData=changeData;[/blue]
//etc etc
}[/tt]
 
You defined the function inside the object as a private function. This means that you can not call it from outside the object. You need to make a privileged function to be able to call it from outside the object. Privileged functions can also be used to call the private functions.

Here's a good page explaining it all:


-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top