今天沒事的時候,研究了一下JS繼承的實現,下面是html的源碼:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>JS類的繼承的實現</title><script type="text/JavaScript">//定義父類及公有、私有、靜態屬性及方法function parent(){var pname = "private";//私有屬性var pfun = function(){//私有方法console.log("調用類的私有方法");}this.getName=function(name){//公有方法this.name = name;//公有屬性return pname+"私有屬性+公有屬性"+this.name+"調用類的共有方法";}}//定義靜態屬性及方法parent.staticPro = "static property";parent.staticFun = function(){var str = "invoke class's static function";return str;}//方法1 原型鏈繼承function childOne(){};childOne.prototype = new parent();//方法2 call/apply繼承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>頁眉</header></body></html>以上就是小編為大家帶來的淺談js繼承的實現及公有、私有、靜態方法的書寫全部內容了,希望大家多多支持VeVb武林網~
新聞熱點
疑難解答