Object oriented JavaScript
From MapbenderWiki
Contents |
constructor
JavaScript is fundamentally about objects. Arrays are objects. Functions are objects. Objects are objects. So what are objects? Objects are collections of name-value pairs. The names are strings, and the values are strings, numbers, booleans, and objects (including arrays and functions).
Let's create a very basic object, maybe a list. This can be something like an array, but with a better interface. There are no classes in Javascript, there are constructors only:
var List = function() {
};
instantiation
You can create a new instance of List by typing
var myList = new List();
members
A member is an attribute of an object.
public
Any function can access, modify, or delete those members, or add new members. You can add a public member in the constructor
var List = function() {
this.list = [];
};
or in the prototype
var List = function() {
};
List.prototype.list = [];
private
If you want to avoid that the member is changed from the outside, consider changing it to private.
var List = function() {
var list = [];
};
Now the member cannot be changed from the outside, but can also not be read. You need a privileged method to achieve read-only.
methods
A method is a function of the object.
privileged
A privileged method is able to access the private variables and methods, and is itself accessible to the public methods and the outside. It is possible to delete or replace a privileged method, but it is not possible to alter it, or to force it to give up its secrets.
var List = function() {
this.get = function(i) {
return list[i];
};
var list = [];
};
add another one to count the elements of the list:
var List = function() {
this.get = function(i) {
return list[i];
};
this.count = function() {
return list.length;
};
var list = [];
};
private
You may want to check if the index passed to the method is correct, you might need method like isIndexValid. As this is of no interest to the outside, let's keep it private: It's only used within the object.
var List = function() {
this.get = function(i) {
if (isValidIndex(i)) {
return list[i];
}
return false;
};
this.count = function() {
return list.length;
};
var isValidIndex = function() {
if (i >= 0 && i < list.length) {
return true;
}
return false;
}
var list = [];
};
You can't use privileged or public methods from within private methods! To do this, you need to add a private variable that references this first. Let's call this variable that.
var List = function() {
this.get = function(i) {
if (isValidIndex(i)) {
return list[i];
}
return false;
};
this.count = function() {
return list.length;
};
var isValidIndex = function() {
if (i >= 0 && i < that.count()) {
return true;
}
return false;
}
var list = [];
var that = this;
};
public
public methods have no access to private members or methods.
var List = function() {
this.get = function(i) {
if (isValidIndex(i)) {
return list[i];
}
return false;
};
this.count = function() {
return list.length;
};
var isValidIndex = function() {
if (i >= 0 && i < that.count()) {
return true;
}
return false;
}
var list = [];
var that = this;
};
List.prototype.toString = function(){
var str = "";
for (var i =0 ; i < this.count() ; i++){
str += this.get(i).toString();
}
return str;
};
Private and privileged members can only be made when an object is constructed. Public members can be added at any time!
I'm not really sure about this, but I believe another advantage of public methods over privileged methods is memory usage: As the public method is attached to the prototype of the object, it is only stored once, while a privileged method is stored in each object!
usage
var myList = new List();
myList.add(10);
myList.add("dog");
myList.add(["a", "very", "big", "array"]);
alert(myList.count()); // alerts: 3
alert(myList); // invokes the toString method and alerts sth. like this: 10dogaverybigarray

