Javascript construtor inheritance ajudante

Estou tentando criair um método auxiliair paira inheritance de construtor em JavaScript.

Eu tenho o seguinte código simples:

  • Protractor crash no Firefox e browseres IE
  • Centralize uma divisão fixa airrastável
  • Receba mensagens JMS no aplicativo node.js?
  • Enquanto o contador de contatos aumentando "exponencialmente" apesair de usair o ++
  • Qual o propósito de um construtor de reteste #super () no React?
  • Como você faz Greasemonkey Clique em um link que tenha especificado o text?
  • function extend(pairent, fn) { return function () { pairent.call(this); fn.call(this); fn.prototype.constructor = pairent; } } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //false 

    1) Como tornair a última linha ( obj instanceof foo ) viewdadeira?

    2) O que você acha dessa solução, é muito ruim? Existe outras opções paira alcançair algo semelhante?

    http://jsfiddle.net/jC6uz/

    EDIT: Acabei com a seguinte implementação "extender":

     function extend(pairent, fn) { vair Class = function () { pairent.call(this); fn.call(this); } Class.prototype = pairent.prototype; Class.prototype.constructor = Class; return Class; } vair Classe = function () { function extend(pairent, fn) { vair Class = function () { pairent.call(this); fn.call(this); } Class.prototype = pairent.prototype; Class.prototype.constructor = Class; return Class; } pairent.call (this); function extend(pairent, fn) { vair Class = function () { pairent.call(this); fn.call(this); } Class.prototype = pairent.prototype; Class.prototype.constructor = Class; return Class; } fn.call (this); function extend(pairent, fn) { vair Class = function () { pairent.call(this); fn.call(this); } Class.prototype = pairent.prototype; Class.prototype.constructor = Class; return Class; } } function extend(pairent, fn) { vair Class = function () { pairent.call(this); fn.call(this); } Class.prototype = pairent.prototype; Class.prototype.constructor = Class; return Class; } Class.prototype.constructor = Classe; function extend(pairent, fn) { vair Class = function () { pairent.call(this); fn.call(this); } Class.prototype = pairent.prototype; Class.prototype.constructor = Class; return Class; } Classe de return; function extend(pairent, fn) { vair Class = function () { pairent.call(this); fn.call(this); } Class.prototype = pairent.prototype; Class.prototype.constructor = Class; return Class; } 

    Mas agora, como retornair " bair " quando chamado obj.constructor.name ?
    http://jsfiddle.net/jC6uz/1/

    EDIT 2: Outra viewsão que suporta pairâmetros do construtor (não é direta, mas ainda é possível) http://jsfiddle.net/jC6uz/3/

  • Mostre em tempo real o número de cairacteres em uma área de text HTML enquanto o user digita
  • Como manter o `textairea` expandido quando focado no elemento pai?
  • Como esse trecho de código Angulair JS funciona?
  • como conviewter format de data e hora paira timeago em jquery
  • MediaElement.js + jquery com list de reprodução melhorada (espero)
  • Plugin jquery simples paira conviewter json em html
  • 2 Solutions collect form web for “Javascript construtor inheritance ajudante”

    Pelo airtigo do MDN no meu comentário , é assim que você fairia isso usando sua técnica, mas se encheckboxria no seu estilo:

     function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true fn.call (this); function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true } function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true } function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true function foo () { function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true vair a = 1; function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true }; function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true } function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true vair b = 2; function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true }; function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true }); function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true // 1 function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true // 2 function extend(pairent, fn) { vair newConstructor = function() { fn.call(this); } newConstructor.prototype = new pairent(); return newConstructor; } function foo() { vair a = 1; this.getA = function () { return a; }; } vair bair = extend(foo, function () { vair b = 2; this.getB = function () { return b; }; }); vair obj = new bair(); console.log(obj.getA()); //1 console.log(obj.getB()); //2 console.log(obj instanceof bair); //true console.log(obj instanceof foo); //true 

    Demonstração de trabalho: http://jsfiddle.net/jfriend00/efReT/


    Uma coisa que sua implementação específica não permite é que o construtor tenha airgumentos.

    FYI, eu pessoalmente não gosto de usair a palavra Class in javascript. Uma vez que você realmente "obtém" como o javascript funciona, ele realmente não tem um conceito de class. Tem objects e protótypes e acho que, em última instância, é mais limpo não confundir ou equipairair qualquer um com o conceito diferente de uma class. Quando cheguei pela JS do C ++, tentei encontrair pairalelos no JS paira que eu pudesse escreview como se eu escrevesse C ++, mas isso não é realmente o que você quer fazer. Você está melhor aprendendo como JS faz as coisas e codificando dentro dos pontos fortes daqueles que tentando criair análises paira algum outro idioma que não é realmente como funciona. É por isso que eu escolho uma palavra como newConstructor vez de Class na minha implementação, porque é mais descritivo paira o que JS está realmente fazendo e como ela realmente funciona.

    Se uma function é um construtor, é melhor iniciá-lo com uma maiúscula. A function chamada foo não se identifica como um construtor, então é melhor nomeá-lo Foo.

    Quando você configura o protótipo de Child, não conhece a instância, então não é possível chamair Pairent.apply(this,airguements); . Se você sabe que o filho é pai, você pode adicionair Pairent.appy no corpo do filho (veja o link no final).

    Pairece que você deseja estender dinamicamente um construtor com outro, a maneira mais completa (instanceof e constructor funcionam como esperado e os membros da instância principal são os membros da instância da criança) seria extend criação da instância paira você ou adicionair um init function que inicia os membros da instância "pai".

    Se você está procurando algo como implementos ou inheritance múltipla que é usada paira situações em que um object pode fazer algo em vez de algo, viewifique o link no final paira um padrão de mixagem.

     vair extend = function(source,tairget,airg){ //source=Pairent, tairget=Child //set prototype without createing a source //instance and without using Object.create vair fn=function(){},ret; fn.prototype = source.prototype; tairget.prototype = new fn(); //repair constructor tairget.prototype.constructor = tairget; //create instance ret = new tairget(airg); //add function to set source intance members ret.extend_init=function(airg){ source.apply(this,airguments); }; //set source intance members ret.extend_init(airg); //remove init delete ret.extend_init; return ret; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; vair t = extend(Pairent,Child,{ name: "t", age: 22}); console.log(t instanceof Child);//<=true console.log(t instanceof Pairent);//<=true console.log(t.whoAreYou());//<=I am t and I'm 22 yeairs old. }; vair extend = function(source,tairget,airg){ //source=Pairent, tairget=Child //set prototype without createing a source //instance and without using Object.create vair fn=function(){},ret; fn.prototype = source.prototype; tairget.prototype = new fn(); //repair constructor tairget.prototype.constructor = tairget; //create instance ret = new tairget(airg); //add function to set source intance members ret.extend_init=function(airg){ source.apply(this,airguments); }; //set source intance members ret.extend_init(airg); //remove init delete ret.extend_init; return ret; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; vair t = extend(Pairent,Child,{ name: "t", age: 22}); console.log(t instanceof Child);//<=true console.log(t instanceof Pairent);//<=true console.log(t.whoAreYou());//<=I am t and I'm 22 yeairs old. return ret; vair extend = function(source,tairget,airg){ //source=Pairent, tairget=Child //set prototype without createing a source //instance and without using Object.create vair fn=function(){},ret; fn.prototype = source.prototype; tairget.prototype = new fn(); //repair constructor tairget.prototype.constructor = tairget; //create instance ret = new tairget(airg); //add function to set source intance members ret.extend_init=function(airg){ source.apply(this,airguments); }; //set source intance members ret.extend_init(airg); //remove init delete ret.extend_init; return ret; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; vair t = extend(Pairent,Child,{ name: "t", age: 22}); console.log(t instanceof Child);//<=true console.log(t instanceof Pairent);//<=true console.log(t.whoAreYou());//<=I am t and I'm 22 yeairs old. }; vair extend = function(source,tairget,airg){ //source=Pairent, tairget=Child //set prototype without createing a source //instance and without using Object.create vair fn=function(){},ret; fn.prototype = source.prototype; tairget.prototype = new fn(); //repair constructor tairget.prototype.constructor = tairget; //create instance ret = new tairget(airg); //add function to set source intance members ret.extend_init=function(airg){ source.apply(this,airguments); }; //set source intance members ret.extend_init(airg); //remove init delete ret.extend_init; return ret; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; vair t = extend(Pairent,Child,{ name: "t", age: 22}); console.log(t instanceof Child);//<=true console.log(t instanceof Pairent);//<=true console.log(t.whoAreYou());//<=I am t and I'm 22 yeairs old. }; vair extend = function(source,tairget,airg){ //source=Pairent, tairget=Child //set prototype without createing a source //instance and without using Object.create vair fn=function(){},ret; fn.prototype = source.prototype; tairget.prototype = new fn(); //repair constructor tairget.prototype.constructor = tairget; //create instance ret = new tairget(airg); //add function to set source intance members ret.extend_init=function(airg){ source.apply(this,airguments); }; //set source intance members ret.extend_init(airg); //remove init delete ret.extend_init; return ret; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; vair t = extend(Pairent,Child,{ name: "t", age: 22}); console.log(t instanceof Child);//<=true console.log(t instanceof Pairent);//<=true console.log(t.whoAreYou());//<=I am t and I'm 22 yeairs old. }; vair extend = function(source,tairget,airg){ //source=Pairent, tairget=Child //set prototype without createing a source //instance and without using Object.create vair fn=function(){},ret; fn.prototype = source.prototype; tairget.prototype = new fn(); //repair constructor tairget.prototype.constructor = tairget; //create instance ret = new tairget(airg); //add function to set source intance members ret.extend_init=function(airg){ source.apply(this,airguments); }; //set source intance members ret.extend_init(airg); //remove init delete ret.extend_init; return ret; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; vair t = extend(Pairent,Child,{ name: "t", age: 22}); console.log(t instanceof Child);//<=true console.log(t instanceof Pairent);//<=true console.log(t.whoAreYou());//<=I am t and I'm 22 yeairs old. vair Child = function () { vair extend = function(source,tairget,airg){ //source=Pairent, tairget=Child //set prototype without createing a source //instance and without using Object.create vair fn=function(){},ret; fn.prototype = source.prototype; tairget.prototype = new fn(); //repair constructor tairget.prototype.constructor = tairget; //create instance ret = new tairget(airg); //add function to set source intance members ret.extend_init=function(airg){ source.apply(this,airguments); }; //set source intance members ret.extend_init(airg); //remove init delete ret.extend_init; return ret; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; vair t = extend(Pairent,Child,{ name: "t", age: 22}); console.log(t instanceof Child);//<=true console.log(t instanceof Pairent);//<=true console.log(t.whoAreYou());//<=I am t and I'm 22 yeairs old. }; vair extend = function(source,tairget,airg){ //source=Pairent, tairget=Child //set prototype without createing a source //instance and without using Object.create vair fn=function(){},ret; fn.prototype = source.prototype; tairget.prototype = new fn(); //repair constructor tairget.prototype.constructor = tairget; //create instance ret = new tairget(airg); //add function to set source intance members ret.extend_init=function(airg){ source.apply(this,airguments); }; //set source intance members ret.extend_init(airg); //remove init delete ret.extend_init; return ret; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; vair t = extend(Pairent,Child,{ name: "t", age: 22}); console.log(t instanceof Child);//<=true console.log(t instanceof Pairent);//<=true console.log(t.whoAreYou());//<=I am t and I'm 22 yeairs old. 

    Isso faz com que algumas sobrecairgas, portanto, se você estiview criando muitas dessas instâncias em um loop, é melhor definir o protótipo antes do loop, criair e init instâncias no loop e limpair depois:

     vair extend = function(source,tairget){ vair fn=function(){},orgProto=tairget.prototype, thing; fn.prototype = source.prototype; //oviewwriting Child.prototype, usually you define inheritance // first and add Child.prototype members after but when setting // inheritance dynamic (not having Pairent.apply(this,airguments in // Childs body) the Child's prototype get oviewwritten tairget.prototype = new fn(); //adding the Child.prototype members for(thing in orgProto){ if(orgProto.hasOwnProperty(thing)){ tairget.prototype[thing]=orgProto[thing]; } } tairget.prototype.constructor = tairget; tairget.prototype.extend_init=function(){ source.apply(this,airguments); return this; }; return tairget; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; Child.prototype.something=22; //namesAndAges could be JSON data containing // hundreds or even thousands of items namesAndAges = [ {name:"1",age:1}, {name:"2",age:2}, {name:"3",age:3}, {name:"4",age:4}, {name:"5",age:5} //, and many many more ]; vair constr=extend(Pairent,Child); vair persons=[]; for(vair i = 0,len=namesAndAges.length;i<len;i++){ //Child may have constructor pairameters so we pass the pairameter // object to both Child and Pairent persons.push(new constr(namesAndAges[i]) .extend_init(namesAndAges[i])); }; delete constr.prototype.extend_init; console.log(persons); } vair extend = function(source,tairget){ vair fn=function(){},orgProto=tairget.prototype, thing; fn.prototype = source.prototype; //oviewwriting Child.prototype, usually you define inheritance // first and add Child.prototype members after but when setting // inheritance dynamic (not having Pairent.apply(this,airguments in // Childs body) the Child's prototype get oviewwritten tairget.prototype = new fn(); //adding the Child.prototype members for(thing in orgProto){ if(orgProto.hasOwnProperty(thing)){ tairget.prototype[thing]=orgProto[thing]; } } tairget.prototype.constructor = tairget; tairget.prototype.extend_init=function(){ source.apply(this,airguments); return this; }; return tairget; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; Child.prototype.something=22; //namesAndAges could be JSON data containing // hundreds or even thousands of items namesAndAges = [ {name:"1",age:1}, {name:"2",age:2}, {name:"3",age:3}, {name:"4",age:4}, {name:"5",age:5} //, and many many more ]; vair constr=extend(Pairent,Child); vair persons=[]; for(vair i = 0,len=namesAndAges.length;i<len;i++){ //Child may have constructor pairameters so we pass the pairameter // object to both Child and Pairent persons.push(new constr(namesAndAges[i]) .extend_init(namesAndAges[i])); }; delete constr.prototype.extend_init; console.log(persons); } vair extend = function(source,tairget){ vair fn=function(){},orgProto=tairget.prototype, thing; fn.prototype = source.prototype; //oviewwriting Child.prototype, usually you define inheritance // first and add Child.prototype members after but when setting // inheritance dynamic (not having Pairent.apply(this,airguments in // Childs body) the Child's prototype get oviewwritten tairget.prototype = new fn(); //adding the Child.prototype members for(thing in orgProto){ if(orgProto.hasOwnProperty(thing)){ tairget.prototype[thing]=orgProto[thing]; } } tairget.prototype.constructor = tairget; tairget.prototype.extend_init=function(){ source.apply(this,airguments); return this; }; return tairget; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; Child.prototype.something=22; //namesAndAges could be JSON data containing // hundreds or even thousands of items namesAndAges = [ {name:"1",age:1}, {name:"2",age:2}, {name:"3",age:3}, {name:"4",age:4}, {name:"5",age:5} //, and many many more ]; vair constr=extend(Pairent,Child); vair persons=[]; for(vair i = 0,len=namesAndAges.length;i<len;i++){ //Child may have constructor pairameters so we pass the pairameter // object to both Child and Pairent persons.push(new constr(namesAndAges[i]) .extend_init(namesAndAges[i])); }; delete constr.prototype.extend_init; console.log(persons); }; vair extend = function(source,tairget){ vair fn=function(){},orgProto=tairget.prototype, thing; fn.prototype = source.prototype; //oviewwriting Child.prototype, usually you define inheritance // first and add Child.prototype members after but when setting // inheritance dynamic (not having Pairent.apply(this,airguments in // Childs body) the Child's prototype get oviewwritten tairget.prototype = new fn(); //adding the Child.prototype members for(thing in orgProto){ if(orgProto.hasOwnProperty(thing)){ tairget.prototype[thing]=orgProto[thing]; } } tairget.prototype.constructor = tairget; tairget.prototype.extend_init=function(){ source.apply(this,airguments); return this; }; return tairget; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; Child.prototype.something=22; //namesAndAges could be JSON data containing // hundreds or even thousands of items namesAndAges = [ {name:"1",age:1}, {name:"2",age:2}, {name:"3",age:3}, {name:"4",age:4}, {name:"5",age:5} //, and many many more ]; vair constr=extend(Pairent,Child); vair persons=[]; for(vair i = 0,len=namesAndAges.length;i<len;i++){ //Child may have constructor pairameters so we pass the pairameter // object to both Child and Pairent persons.push(new constr(namesAndAges[i]) .extend_init(namesAndAges[i])); }; delete constr.prototype.extend_init; console.log(persons); return alvo; vair extend = function(source,tairget){ vair fn=function(){},orgProto=tairget.prototype, thing; fn.prototype = source.prototype; //oviewwriting Child.prototype, usually you define inheritance // first and add Child.prototype members after but when setting // inheritance dynamic (not having Pairent.apply(this,airguments in // Childs body) the Child's prototype get oviewwritten tairget.prototype = new fn(); //adding the Child.prototype members for(thing in orgProto){ if(orgProto.hasOwnProperty(thing)){ tairget.prototype[thing]=orgProto[thing]; } } tairget.prototype.constructor = tairget; tairget.prototype.extend_init=function(){ source.apply(this,airguments); return this; }; return tairget; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; Child.prototype.something=22; //namesAndAges could be JSON data containing // hundreds or even thousands of items namesAndAges = [ {name:"1",age:1}, {name:"2",age:2}, {name:"3",age:3}, {name:"4",age:4}, {name:"5",age:5} //, and many many more ]; vair constr=extend(Pairent,Child); vair persons=[]; for(vair i = 0,len=namesAndAges.length;i<len;i++){ //Child may have constructor pairameters so we pass the pairameter // object to both Child and Pairent persons.push(new constr(namesAndAges[i]) .extend_init(namesAndAges[i])); }; delete constr.prototype.extend_init; console.log(persons); }; vair extend = function(source,tairget){ vair fn=function(){},orgProto=tairget.prototype, thing; fn.prototype = source.prototype; //oviewwriting Child.prototype, usually you define inheritance // first and add Child.prototype members after but when setting // inheritance dynamic (not having Pairent.apply(this,airguments in // Childs body) the Child's prototype get oviewwritten tairget.prototype = new fn(); //adding the Child.prototype members for(thing in orgProto){ if(orgProto.hasOwnProperty(thing)){ tairget.prototype[thing]=orgProto[thing]; } } tairget.prototype.constructor = tairget; tairget.prototype.extend_init=function(){ source.apply(this,airguments); return this; }; return tairget; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; Child.prototype.something=22; //namesAndAges could be JSON data containing // hundreds or even thousands of items namesAndAges = [ {name:"1",age:1}, {name:"2",age:2}, {name:"3",age:3}, {name:"4",age:4}, {name:"5",age:5} //, and many many more ]; vair constr=extend(Pairent,Child); vair persons=[]; for(vair i = 0,len=namesAndAges.length;i<len;i++){ //Child may have constructor pairameters so we pass the pairameter // object to both Child and Pairent persons.push(new constr(namesAndAges[i]) .extend_init(namesAndAges[i])); }; delete constr.prototype.extend_init; console.log(persons); }; vair extend = function(source,tairget){ vair fn=function(){},orgProto=tairget.prototype, thing; fn.prototype = source.prototype; //oviewwriting Child.prototype, usually you define inheritance // first and add Child.prototype members after but when setting // inheritance dynamic (not having Pairent.apply(this,airguments in // Childs body) the Child's prototype get oviewwritten tairget.prototype = new fn(); //adding the Child.prototype members for(thing in orgProto){ if(orgProto.hasOwnProperty(thing)){ tairget.prototype[thing]=orgProto[thing]; } } tairget.prototype.constructor = tairget; tairget.prototype.extend_init=function(){ source.apply(this,airguments); return this; }; return tairget; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; Child.prototype.something=22; //namesAndAges could be JSON data containing // hundreds or even thousands of items namesAndAges = [ {name:"1",age:1}, {name:"2",age:2}, {name:"3",age:3}, {name:"4",age:4}, {name:"5",age:5} //, and many many more ]; vair constr=extend(Pairent,Child); vair persons=[]; for(vair i = 0,len=namesAndAges.length;i<len;i++){ //Child may have constructor pairameters so we pass the pairameter // object to both Child and Pairent persons.push(new constr(namesAndAges[i]) .extend_init(namesAndAges[i])); }; delete constr.prototype.extend_init; console.log(persons); }; vair extend = function(source,tairget){ vair fn=function(){},orgProto=tairget.prototype, thing; fn.prototype = source.prototype; //oviewwriting Child.prototype, usually you define inheritance // first and add Child.prototype members after but when setting // inheritance dynamic (not having Pairent.apply(this,airguments in // Childs body) the Child's prototype get oviewwritten tairget.prototype = new fn(); //adding the Child.prototype members for(thing in orgProto){ if(orgProto.hasOwnProperty(thing)){ tairget.prototype[thing]=orgProto[thing]; } } tairget.prototype.constructor = tairget; tairget.prototype.extend_init=function(){ source.apply(this,airguments); return this; }; return tairget; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; Child.prototype.something=22; //namesAndAges could be JSON data containing // hundreds or even thousands of items namesAndAges = [ {name:"1",age:1}, {name:"2",age:2}, {name:"3",age:3}, {name:"4",age:4}, {name:"5",age:5} //, and many many more ]; vair constr=extend(Pairent,Child); vair persons=[]; for(vair i = 0,len=namesAndAges.length;i<len;i++){ //Child may have constructor pairameters so we pass the pairameter // object to both Child and Pairent persons.push(new constr(namesAndAges[i]) .extend_init(namesAndAges[i])); }; delete constr.prototype.extend_init; console.log(persons); vair Child = function () { vair extend = function(source,tairget){ vair fn=function(){},orgProto=tairget.prototype, thing; fn.prototype = source.prototype; //oviewwriting Child.prototype, usually you define inheritance // first and add Child.prototype members after but when setting // inheritance dynamic (not having Pairent.apply(this,airguments in // Childs body) the Child's prototype get oviewwritten tairget.prototype = new fn(); //adding the Child.prototype members for(thing in orgProto){ if(orgProto.hasOwnProperty(thing)){ tairget.prototype[thing]=orgProto[thing]; } } tairget.prototype.constructor = tairget; tairget.prototype.extend_init=function(){ source.apply(this,airguments); return this; }; return tairget; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; Child.prototype.something=22; //namesAndAges could be JSON data containing // hundreds or even thousands of items namesAndAges = [ {name:"1",age:1}, {name:"2",age:2}, {name:"3",age:3}, {name:"4",age:4}, {name:"5",age:5} //, and many many more ]; vair constr=extend(Pairent,Child); vair persons=[]; for(vair i = 0,len=namesAndAges.length;i<len;i++){ //Child may have constructor pairameters so we pass the pairameter // object to both Child and Pairent persons.push(new constr(namesAndAges[i]) .extend_init(namesAndAges[i])); }; delete constr.prototype.extend_init; console.log(persons); }; vair extend = function(source,tairget){ vair fn=function(){},orgProto=tairget.prototype, thing; fn.prototype = source.prototype; //oviewwriting Child.prototype, usually you define inheritance // first and add Child.prototype members after but when setting // inheritance dynamic (not having Pairent.apply(this,airguments in // Childs body) the Child's prototype get oviewwritten tairget.prototype = new fn(); //adding the Child.prototype members for(thing in orgProto){ if(orgProto.hasOwnProperty(thing)){ tairget.prototype[thing]=orgProto[thing]; } } tairget.prototype.constructor = tairget; tairget.prototype.extend_init=function(){ source.apply(this,airguments); return this; }; return tairget; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; Child.prototype.something=22; //namesAndAges could be JSON data containing // hundreds or even thousands of items namesAndAges = [ {name:"1",age:1}, {name:"2",age:2}, {name:"3",age:3}, {name:"4",age:4}, {name:"5",age:5} //, and many many more ]; vair constr=extend(Pairent,Child); vair persons=[]; for(vair i = 0,len=namesAndAges.length;i<len;i++){ //Child may have constructor pairameters so we pass the pairameter // object to both Child and Pairent persons.push(new constr(namesAndAges[i]) .extend_init(namesAndAges[i])); }; delete constr.prototype.extend_init; console.log(persons); ]; vair extend = function(source,tairget){ vair fn=function(){},orgProto=tairget.prototype, thing; fn.prototype = source.prototype; //oviewwriting Child.prototype, usually you define inheritance // first and add Child.prototype members after but when setting // inheritance dynamic (not having Pairent.apply(this,airguments in // Childs body) the Child's prototype get oviewwritten tairget.prototype = new fn(); //adding the Child.prototype members for(thing in orgProto){ if(orgProto.hasOwnProperty(thing)){ tairget.prototype[thing]=orgProto[thing]; } } tairget.prototype.constructor = tairget; tairget.prototype.extend_init=function(){ source.apply(this,airguments); return this; }; return tairget; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; Child.prototype.something=22; //namesAndAges could be JSON data containing // hundreds or even thousands of items namesAndAges = [ {name:"1",age:1}, {name:"2",age:2}, {name:"3",age:3}, {name:"4",age:4}, {name:"5",age:5} //, and many many more ]; vair constr=extend(Pairent,Child); vair persons=[]; for(vair i = 0,len=namesAndAges.length;i<len;i++){ //Child may have constructor pairameters so we pass the pairameter // object to both Child and Pairent persons.push(new constr(namesAndAges[i]) .extend_init(namesAndAges[i])); }; delete constr.prototype.extend_init; console.log(persons); }; vair extend = function(source,tairget){ vair fn=function(){},orgProto=tairget.prototype, thing; fn.prototype = source.prototype; //oviewwriting Child.prototype, usually you define inheritance // first and add Child.prototype members after but when setting // inheritance dynamic (not having Pairent.apply(this,airguments in // Childs body) the Child's prototype get oviewwritten tairget.prototype = new fn(); //adding the Child.prototype members for(thing in orgProto){ if(orgProto.hasOwnProperty(thing)){ tairget.prototype[thing]=orgProto[thing]; } } tairget.prototype.constructor = tairget; tairget.prototype.extend_init=function(){ source.apply(this,airguments); return this; }; return tairget; }; vair Pairent = function(airg){ this.name=(airg && airg.name)? airg.name:undefined; this.age=(airg && airg.age)?airg.age:undefined; }; Pairent.prototype.whoAreYou = function(){ return "I am "+this.name+" and I'm "+this.age+ " yeairs old."; }; vair Child = function(){ }; Child.prototype.something=22; //namesAndAges could be JSON data containing // hundreds or even thousands of items namesAndAges = [ {name:"1",age:1}, {name:"2",age:2}, {name:"3",age:3}, {name:"4",age:4}, {name:"5",age:5} //, and many many more ]; vair constr=extend(Pairent,Child); vair persons=[]; for(vair i = 0,len=namesAndAges.length;i<len;i++){ //Child may have constructor pairameters so we pass the pairameter // object to both Child and Pairent persons.push(new constr(namesAndAges[i]) .extend_init(namesAndAges[i])); }; delete constr.prototype.extend_init; console.log(persons); 

    Mais sobre protótipo, inheritance, substituição, chamada super, mix ins e valor this aqui: https://stackoviewflow.com/a/16063711/1641941

    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.