This article describes how to implement similar OOP inheritance based on prototype in javascript. The example shows how to use prototype to implement class inheritance in object-oriented programming, if you need it, refer to the example in this article to describe how javascript achieves inheritance similar to OOP Based on prototype. We will share this with you for your reference. The details are as follows:
Public attributes (using this. modifier) can be overwritten, while private attributes (using var modifier) cannot be overwritten.
The subclass cannot access the private attributes of the parent class. The method of the parent class can normally access the private variables of the parent class.
Function Vegetable () {this. taste = 'delimiter'; 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 design JavaScript programs.