//Function Constructor : Person has an instance property : name function Person(name){ this.name = name; } //Person Instances let john = new Person('John'); let sara = new Person('Sara'); console.log(john.name); //John console.log(sara.name); //Sara Person.prototype.walk = function(){ console.log(this.name + ' is walking.'); } john.walk(); //John is walking sara.walk(); //Sara is walking //Programmer Constructor : Wants to inherit Person function Programmer(name, programmingLanguage){ Person.call(this, name); this.programmingLanguage = programmingLanguage || ''; } Programmer.prototype = Object.create(Person.prototype); Programmer.prototype.constructor = Programmer; var snig = new Programmer('Snigdha', 'Javascript'); console.log(snig.name); console.log(snig.programmingLanguage); snig.walk();
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter