it:ad:jscript:private_fields

IT:AD:JScript:Private Fields

Prototypes are not superclasses. They are separate objects.

Won't work:

function Shape() {
  var area = 50;
  this.setArea = function(a) {area = a;};
  this.getArea = function() {return area;};
}

function Square(){}
Square.prototype = Shape;

var shape1 = new Square();
var shape2 = new Square();
shape1.setArea(100);
//Unfortunately, shape2.getArea==100, when it should be 50...

Works:

function Square(){}
  Shape.call(this);
}

//OR

function Square(){}
  this.Shape = Shape;
  this.Shape();
}


var shape1 = new Square();
var shape2 = new Square();
shape1.setArea(100);
//Correct: shape2.getArea==50...
  • /home/skysigal/public_html/data/pages/it/ad/jscript/private_fields.txt
  • Last modified: 2023/11/04 22:28
  • by 127.0.0.1