2013年7月12日 星期五

Javascript Prototype Chain / Inheritance

*Every Object has an internal link to another object called prototype
syntax constructs:
var o = {a: 1};
//o ---> Object.prototype ---> null

var a = ["yo", "whadup", "?"];
//a ---> Array.prototype ---> Object.prototype ---> null

function f(){
return 2;
}
//f ---> Function.prototype ---> Object.prototype ---> null

Constructor:
function Graph() {
this.vertexes = [];
this.edges = [];
}
Graph.prototype = {
addVertex: function(v){
this.vertexes.push(v);
}
};
var g = new Graph();
//Graph ---> Graph.prototype ---> Object.prototype


Object.create
var a = {a: 1};
var b = Object.create(a);
// b ---> a ---> Object.prototype ---> null
var c = Object.create(b);
// c ---> b ---> a ---> Object.prototype ---> null
var d = Object.create(null);
// d ---> null

沒有留言:

張貼留言