SoFunction
Updated on 2025-04-13

A simple example of javascript class definition


<script>
//Define a javascript class
function JsClass(privateParam/* */,publicParam){//Constructor
var primary = privateParam; //Private variable
= publicParam; //Public variable
//Define private methods
function priMethod(){
return "priMethod()";
}
//Define privileged method
//Private method can access all members
= function(){
var str = "This is a privileged method, I called \n";
str += " Private variable: " + priMember +"\n";
str += " Private method: " + priMethod() +"\n";
str += " Public variable: " + +"\n";
str += " Public method: " + ();

return str;
}
}
//Add a public method
//Private variables and methods cannot be called
= function(){
return "pubMethod()";
}

//Example using JsClass
JsObject = new JsClass("priMember","pubMember");

//alert();//Popt out pubMember information
//alert();//Undefined information pops up
//alert(());//Popt out pubMethod information
//alert(());//The error pops up "The object does not support this property or method"
alert(());
</script>