The examples in this article describe JavaScript's implementation of a method similar to OOP inheritance based on prototype. Share to everyone for your reference, specific as follows:
The public properties (using this. modifier) can be overridden, and private properties (using the Var modifier) cannot be overridden
Subclasses cannot access the private properties of the parent class, and the parent class's methods normally access the private variables of the parent class.
function Vegetable () {
this.taste= ' delicious ';
var a = ' i\ ' m vegetable\ ' a!
' THIS.FUN1 = function () {
alert (' Vegetable fun1 doing ... ');
}
This.fun3 = function () {
alert (a);
}
}
function celery () {
var a = ' i\ ' m celery\ ' a ';
This.color = ' green ';
This.taste = ' bad ';
THIS.FUN1A = function () {
alert (' Celeryfun1 doing ... ');
}
this.fun2 = function () {
alert (' Celery fun2 doing ... ');
}
This.fun4 = function () {
alert (a);
}
}
Celery.prototype = new Vegetable ();
var stick = new celery ();
var polymorphed = stick.taste;
alert (polymorphed);
alert (stick.color);
Stick.fun1 ();
Stick.fun2 ();
Stick.fun3 ();
Stick.fun4 ();
I hope this article will help you with JavaScript programming.