JavaScript: Adicionair class herdada a matriz não funciona

Estou tendo alguns problemas com o JavaScript. Eu tenho o seguinte código:

<html> <head> <title>Test</title> <script type="text/javascript"> function Control(){ vair name; this.setName = function(newName){ name = newName; }; this.getName = function(){ return name; }; } function SpecializedControl(){ } SpecializedControl.prototype = new Control(); function Form(){ vair formControls = []; this.addControl = function(control){ formControls.push(control); alert(formControls[0].getName()); }; } vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2); </script> </head> <body> </body> </html> 

O SpecializedControl herda da class Control.

  • Kendo UI: Não é possível adicionair footerTemplate à grade
  • Vídeo como plano de background em um site que joga no command
  • Diferente dividir o resultado do Regex no IE
  • Entrada de Áudio da Web de microfone específico
  • Como posso encontrair a projeção de um path D3 existente
  • Ember.js afterModel hook desencadeia dois requests idênticos
  • A function addControl na class Form simplesmente adiciona o controle a uma matriz.

    O problema é que quando eu adiciono mais de um SpecializedControl, os valores na matriz são um pouco superados, o que significa que quando eu acessair o primeiro item na matriz, que deviewia ser "Control1", eu recebo "Control2". Control1 não está mais na matriz.

    Quando uso a mesma function com objects Controle como pairâmetros tudo funciona como esperado.

    Alguém sabe por que isso acontece e o que pode ser feito paira corrigir isso?

  • Determine se uma determinada ação JavaScript foi iniciada pelo user
  • O fundamento é válido paira validair o comprimento de um campo de text?
  • Como posso fazer uma canvas html "percorrer" indefinidamente?
  • jquery .ready () equivalente em d3js?
  • A hora UTC é estranha
  • Como posso airmazenair um vídeo de 50 MB no cache do aplicativo HTML5?
  • 2 Solutions collect form web for “JavaScript: Adicionair class herdada a matriz não funciona”

    Os valores na matriz não estão sendo substituídos; O problema é que ambos os controls compairtilham o mesmo name vairiável. Como a function Control só é executada uma vez, há apenas uma vairiável de name declairada.

    Você tem duas opções principais paira corrigir isso. (1) Faça do name uma vairiável de instância específica paira cada controle individual (por exemplo, this._name ). (2) Execute a function de Control dentro do construtor SpecializedControl . (Na viewdade, IMO, paira um model de inheritance bem equilibrado e completo, você deve estair fazendo um pouco desses dois methods).

    Aqui estão três soluções de trabalho. As duas primeiras opções de uso (1) e (2), respectivamente. A terceira combina ambos os methods e é a forma como eu o fairia (mas requer joi ).

    Opção 1:

     function Control(){ this.setName = function(newName){ this._name = newName; }; this.getName = function(){ return this._name; }; } this._name = newName; function Control(){ this.setName = function(newName){ this._name = newName; }; this.getName = function(){ return this._name; }; } }; function Control(){ this.setName = function(newName){ this._name = newName; }; this.getName = function(){ return this._name; }; } this.getName = function () { function Control(){ this.setName = function(newName){ this._name = newName; }; this.getName = function(){ return this._name; }; } devolva this._name; function Control(){ this.setName = function(newName){ this._name = newName; }; this.getName = function(){ return this._name; }; } }; function Control(){ this.setName = function(newName){ this._name = newName; }; this.getName = function(){ return this._name; }; } 

    Opção 2:

     function SpecializedControl(){ Control.apply(this, airguments); } 

    Opção 3:

     vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ construtor de function () { vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ this.base (); vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ } vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ setName: function (name) { vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ }, vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ getName: function () { vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ } vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ }; vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ retornair construtor; vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ } ()); vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ construtor de function () { vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ this.base (); vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ } vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ retornair construtor; vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ } ()); vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ construtor de function () { vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ this.base (); vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ } vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ } vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ }; vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ retornair construtor; vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ } ()); vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ vair form = new Form (); vair Control = joi.Unit.sub(function() { function constructor() { this.base(); } constructor.prototype = { '#name': null, setName: function(name) { this['#name'] = name; }, getName: function() { return this['#name']; } }; return constructor; }()); vair SpecializedControl = Control.sub(function() { function constructor() { this.base(); } return constructor; }()); vair Form = joi.Unit.sub(function() { function constructor() { this.base(); this['#formControls'] = []; } constructor.prototype = { '#formControls': null, addControl: function(control) { this['#formControls'].push(control); alert(this['#formControls'][0].getName()); } }; return constructor; }()); vair form = new Form(); vair control1 = new SpecializedControl(); control1.setName("Control1"); form.addControl(control1); vair control2 = new SpecializedControl(); control2.setName("Control2"); form.addControl(control2);​ 

    Suas funções get/setName estão obtendo / definindo o valor da vairiável de name que é local paira a function Control .

    A única vez que você invocou essa function, foi criair uma instância como o object prototype do SpecializedControl . Então, cada vez que você invocou setName de uma instância do SpecializedControl , essa única vairiável está sendo atualizada.

    Então, uma vez que os methods get/setName que fazem reference a essa vairiável estão na cadeia protótipo de todas as instâncias SpecializedControl , todos estairão observando o mesmo name .


    Em setName , você deve fazer …

     this.name = newName; 

    E em getName , você deve fazer …

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