English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
I studied the implementation of JS inheritance today when I had nothing to do, and here ishtmlSource code:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Implementation of JS Class Inheritance</title>/title> <script type="text/JavaScript"> //Define the superclass and public, private, static properties and methods function parent() { var pname = "private";//Private property var pfun = function() {//Private method console.log("Calling a private method of the class"); } this.getName = function(name) {//public method this.name = name;//public property return pname+"private property+public property+this.name+"invoke the class's public method"; } } //Define static properties and methods parent.staticPro = "static property"; parent.staticFun = function(){ var str = "invoke class's static function"; return str; } //method1 prototype chain inheritance function childOne(){}; childOne.prototype = new parent(); //method2 call/apply inheritance function childTwo(){ parent.call(this); } function init(){ var c1 = new childOne(); console.log(c1.getName("child1"));// console.log(c1.name); var c2 = new childTwo(); console.log(c2.getName("child2")); console.log(c2.name); console.log(parent.staticPro); console.log(parent.staticFun()); } </script> </head> <body onload="init();"> <header>Page Header</header>/header> </body> </html>
That's all for the brief introduction to the implementation of js inheritance and the writing of public, private, and static methods brought to you by the editor. I hope everyone will support and cheer for the tutorial~