prototype chain


Javascript Basic

prototype

function Ultra(){}
Ultra.prototype.ultraProp = true;

function Super(){}
Super.prototype = new Ultra();

function Sub(){}
Sub.prototype = new Super();

var o = new Sub();
console.log(o.ultraProp);
// Result : true
// because Sub has ultraProp? no ->
// Super has ultraProp? no ->
// Ultra has ultraProp? Yes ->
// value? -> true
function Ultra(){}
Ultra.prototype.ultraProp = true;

function Super(){}
Super.prototype = new Ultra();

function Sub(){}
Sub.prototype = new Super();
Sub.prototype.ultraProp = 2;

var o = new Sub();
console.log(o.ultraProp);
// Result : 2
// Sub has ultraProp? Yes ->
// Value? -> 2

another example

function Ultra(){}
Ultra.prototype.ultraProp = true;

function Super(){}
Super.prototype = new Ultra();

function Sub(){}
var s = new Super();
s.ultraProp = 3;
Sub.prototype = s;
//it means Sub.prototype.ultraProp = 3;
// Sub.prototype = new Super();
// if we remove s, we can understand it's same with above codes.

var o = new Sub();
console.log(o.ultraProp);





© 2017. by isme2n

Powered by aiden