新聞中心
本文實(shí)例講述了JS仿Base.js實(shí)現(xiàn)的繼承。分享給大家供大家參考,具體如下:
成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作介紹好的網(wǎng)站是理念、設(shè)計(jì)和技術(shù)的結(jié)合。成都創(chuàng)新互聯(lián)擁有的網(wǎng)站設(shè)計(jì)理念、多方位的設(shè)計(jì)風(fēng)格、經(jīng)驗(yàn)豐富的設(shè)計(jì)團(tuán)隊(duì)。提供PC端+手機(jī)端網(wǎng)站建設(shè),用營銷思維進(jìn)行網(wǎng)站設(shè)計(jì)、采用先進(jìn)技術(shù)開源代碼、注重用戶體驗(yàn)與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺化效果。
var Klass = function() {}; Klass.extendClass = (function() { var F = function() {}; return function(C, P) { F.prototype = P.prototype; C.prototype = new F(); C.uper = P.prototype; C.prototype.constructor = C; }; })(); Klass.extend = function(props) { var _slice = Array.prototype.slice; var Glass = function() { /*if (Glass.uper && Glass.uper.hasOwnProperty("init")) { Glass.uper.init.apply(this, _slice.call(arguments)) }*/ if (Glass.prototype.hasOwnProperty("init")) { Glass.prototype.init.apply(this, _slice.call(arguments)); } }; Klass.extendClass(Glass, this); Glass.extend = this.extend; for (var key in props) { if (props.hasOwnProperty(key)) { Glass.prototype[key] = props[key]; } } return Glass; };
example:
var A = Klass.extend({ init: function(name) { this.name = name; console.log('A constructor is running!'); }, getName: function() { return this.name; } }); var B = A.extend({ init: function(name) { this.name = name; console.log('B constructor is running!'); }, getName: function() { return this.name; }, a: 'b' }); var C = B.extend({ init: function(name) { console.log('C constructor is running!'); }, c: 'c', getName: function() { var name = C.uper.getName.call(this); return 'Hi, I\'m' + this.name; } }); var c1 = new C('zlf'); console.log(c1.getName());
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
本文題目:JS仿Base.js實(shí)現(xiàn)的繼承示例
當(dāng)前鏈接:http://www.dlmjj.cn/article/joosph.html