當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
【JavaScript】JavaScript模拟Class
生活随笔
收集整理的這篇文章主要介紹了
【JavaScript】JavaScript模拟Class
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
beauty("$Class",["$underscore"],function(_){var Class = function () {var length = arguments.length;var option = arguments[length - 1];option.init = option.init || function () {};// 如果參數中有要繼承的父類if (length === 2) {/*** @ignore*/var superClass = arguments[0].extend;/*** @ignore*/var tempClass = function () {};tempClass.prototype = superClass.prototype;/*** @ignore*/var subClass = function () {this.init.apply(this, arguments);};// 加一個對父類原型引用的靜態屬性subClass.superClass = superClass.prototype;//subClass.superClass = superClass;/*** @ignore*/subClass.callSuper = function (context, func) {var slice = Array.prototype.slice;var a = slice.call(arguments, 2);var func = subClass.superClass[func];//var func = subClass.superClass.prototype[func];if (func) {func.apply(context, a.concat(slice.call(arguments)));}};// 指定原型subClass.prototype = new tempClass();// 重新指定構造函數subClass.prototype.constructor = subClass;_.extend(subClass.prototype, option);/*** @ignore*/subClass.prototype.init = function () {// 調用父類的構造函數// subClass.superClass.init.apply(this, arguments);// 調用此類自身的構造函數option.init.apply(this, arguments);};return subClass;// 如果參數中沒有父類,則單純構建一個類} else {if (length === 1) {/*** @ignore*/var newClass = function () {// 加了return,否則init返回的對象不生效return this.init.apply(this, arguments);};newClass.prototype = option;return newClass;}}};beauty.$superPackage("$Class",Class);});
轉載于:https://www.cnblogs.com/lhp2012/p/4809421.html
總結
以上是生活随笔為你收集整理的【JavaScript】JavaScript模拟Class的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上周热点回顾(9.7-9.13)
- 下一篇: 解决yum错误Error: reques