Created
December 27, 2013 03:31
-
-
Save zhangwc/8142246 to your computer and use it in GitHub Desktop.
实现继承机制,js文件加载时,立即执行
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* クラス間のプロトタイプによる継承関係を定義する。<br> | |
* このメソッドで継承関係が定義されたクラスには、継承元クラスを参照するsuperClassプロパティが追加される。 | |
* @param {Class} subClass 継承先のクラス | |
* @param {Class} [superClass=MeObject] 継承元のクラス | |
* @param {String} [alias] toStringメソッドに表示されるクラス名 | |
*/ | |
MeObject.inherit = function(subClass, superClass, alias) { | |
superClass = superClass || MeObject; | |
var proxy = function() {}; | |
proxy.prototype = superClass.prototype; | |
subClass.superClass = superClass; | |
var orgProto = subClass.prototype; | |
var proto = subClass.prototype = new proxy(); | |
proto.constructor = subClass; | |
//既にメソッドが宣言されているケースを想定し、メンバをコピーする。 | |
for (var mem in orgProto) { | |
if (orgProto.hasOwnProperty(mem)) { | |
proto[mem] = orgProto[mem]; | |
} | |
} | |
if (!alias) { | |
var m = subClass.toString().match(/^function\s+([^\s]+)\s*\(/); | |
alias = m ? m[1] : "Object"; | |
} | |
proto.alias = alias; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment