JavaScript

OOP4: A hole in prototype chain

function SuperType(){ this.a = 1; this.b = [2]; } SuperType.prototype.c = 3; SuperType.prototype.d = [4]; function SubType(){} SubType.prototype = new SuperType(); var instance1 = new SubType(); instance1.a = ‘s’; instance1.b.push(‘s’); instance1.c = ‘s’; instance1.d.push(‘s’); var instance2 = new SuperType(); console.log(instance2.a);//1 console.log(instance2.b);//[2] console.log(instance2.c);//3 console.log(instance2.d);//[4,’s’] For base type, this.a or SuperType.prototype.c all work like below: var a… Continue reading OOP4: A hole in prototype chain

JavaScript

OOP3: How new works

Student.prototype = new Person(); var student = new Student(); What’s the difference between above two? Firstly, let’s see what new does. 1.create an object 2.set its _proto_ pointing to Person.prototype. 3.Person.apply(object, arguments) In the first case, Student.prototype points to the object that can use all the methods and properties inherited from Person. While Student is… Continue reading OOP3: How new works