Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
Object inheritance with a knockout property

I have the following js inheritance example:

function Fruit(){
  this.who = function(){ console.dir(this.fruitName); }
}
Fruit.prototype.fruitName = "I am a fruit";

function Orange(){
  Fruit.call();
  this.fruitName = "I am an orange";
}
Orange.prototype = new Fruit();
Orange.prototype.constructor = Orange;

function Apple(){
  Fruit.call();
  this.fruitName = "I am an apple";
}
Apple.prototype = new Fruit();
Apple.prototype.constructor = Orange;

var orange = new Orange();
var apple = new Apple();
orange.who();
apple.who();

The code above outputs:

I am an orange
I am an apple

Which is correct.

Now, changing the fruitName to a knockout observable gives an unexpected result:

function Fruit(){
  this.who = function(){ console.dir(this.fruitName()); }
}
Fruit.prototype.fruitName = ko.observable("I am a fruit");

function Orange(){
  Fruit.call();
  this.fruitName("I am an orange");
}
Orange.prototype = new Fruit();
Orange.prototype.constructor = Orange;

function Apple(){
  Fruit.call();
  this.fruitName("I am an apple");
}
Apple.prototype = new Fruit();
Apple.prototype.constructor = Orange;

var orange = new Orange();
var apple = new Apple();
orange.who();
apple.who();

Output:

I am an apple
I am an apple

Unless I am doing something wrong, this looks like a bug in Knockout. Is there a way around this issue?

Full jsfiddle available here: https://jsfiddle.net/h1go9se2/

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What's going on

The idea of the prototype is that it's shared between instances. Functions use this to make sure running the methods actually impacts a single instance.

Knockout observables are functions, but they're best thought of as wrappers around a value.

If you define your observable on the prototype, it means you're wrapping a value to be shared by all instances.

Writing to that value in the constructor of any class that extends it will overwrite the value for all instances.

The fix

Instead of declaring fruitName on the Fruit prototype, you define the property inside the Fruit constructor:

function Fruit(){
  this.who = function(){ console.dir(this.fruitName()); }
  this.fruitName = ko.observable("I am a fruit");
}

Runnable snippet:

function Fruit(){
  this.who = function(){ console.dir(this.fruitName()); }
  this.fruitName = ko.observable("I am a fruit");
}

function Orange(){
  Fruit.call();
  this.fruitName("I am an orange");
}
Orange.prototype = new Fruit();
Orange.prototype.constructor = Orange;

function Apple(){
  Fruit.call();
  this.fruitName("I am an apple");
}
Apple.prototype = new Fruit();
Apple.prototype.constructor = Orange;

var orange = new Orange();
var apple = new Apple();
orange.who();
apple.who();
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda