inheritance - Prototypal Inheritence in Javascript -


how pass values super class constructors in javascript.

in following code snippet

(function wrapper() { var id = 0;  window.testclass = testclass;  function testclass() {     id++;     this.name = 'name_' + id;     function privatefunc() {         console.log(name);     }     function publicfunc() {         privatefunc();     }      this.publicfunc = publicfunc; }  function teststring() {     this.getname = function() {         console.log("***" + this.name + "****************");     } }  testclass.prototype = new teststring();  })(); 

how pass values teststring constructor? currently, super class method using value using 'this' keyword. there way directly pass values super class constructor. also, want see simple example of extending string. demystify lot of things.

you can use following code in constructor of testclass().

teststring.apply(this, arguments); 

this call constructor new object context.

however, note establishing [[prototype]] chain testclass.prototype = new teststring(); have called constructor once.

jsfiddle.

i want see simple example of extending string.

you can augment prototype property.

string.prototype.endswidth = function(str) {     return this.slice(-str.length) === str; }; 

Comments