import syeef.com.*;
public class JavaScript {
//

Object-oriented Programming

This example shows how to do Object-oriented Programming in JavaScript:


function Ball(arg) {
var name = arg; // var means private
this.x = 0; // this means public
this.y = 0;
this.setName = function(arg) {
name = arg;
};
this.getName = function() {
return name;
};
}

// new Object of Ball with name "Blue Ball"
var blue = new Ball("Blue Ball");
// another Object of Ball with name "Red Ball"
var red = new Ball("Red Ball");

console.log(blue.getName()); // output "Blue Ball"
console.log(red.getName()); // output "Red Ball"
red.setName("Black Ball");
console.log(red.getName()); // output "Black Ball"
console.log(blue.getName()); // output "Blue Ball", unaffected

blue.x = 10; // set blue ball x to 10
console.log(blue.x); // get blue ball x
}