Thursday, February 11, 2016

Javascript Object Oriented

<html>
<head>
<script>
function Say() {
//private
var nameA = "ha";
//public
this.nameB = "he";
//private
function sayBye() {
return "Bye!" + nameA;
}
//Privileged -> cannot be shared by all instances
this.sayHello = function() {
return "Hello!" + nameA;
}
}
//static
Say.nameC = "wa";
//static
Say.sayBye = function() {
return "Bye!" + this.nameC;
}
//public -> can be shared by all instances
Say.prototype.sayHi = function() {
return "Hi!" + this.nameB;
}
var a = new Say();
a.sayWa = function ()
{
return "Wa!" + a.nameB;
}
alert(a.sayHello());
alert(a.sayHi());
alert(Say.sayBye());
alert(a.sayWa());
var b = new Say();
alert(b.sayHi());
alert(b.sayWa());  //throw exception
</script>
</head>
<body>
</body>
</html>