Javascriptは対象拡張ライブラリコードに向かって共有します.

15299 ワード

lang.jsライブラリは、パッケージとクラスの定義、クラスの継承と混合、関数の再負荷などの機能を提供しています.ほとんどの対象に向けて設計されたニーズを満たすことができます.同時にチェーンベースの定義方式を支持して、倉庫を使用する時に更に規範と便利にします.まず簡単な例を通してlang.jsの基本機能を示しました.その後、lang.jsのソースコードと注釈を提供しました.
一.機能紹介
「lang」はフレームのグローバル定義として機能し、4つの方法が含まれている.
lang.Package/パケットを定義するために使用されます(デフォルトは全体に露出されます).
lang.class(string name[,object config],object classBody]//定義類に用いる
lang.Object/リロード関数をサポートする一般的なオブジェクトを定義します.
lang.Funct/リロード関数を定義するために使用されます.
 
  
var lang = (function(){
/***********************************
Javascript (lang.js v1.0)
By: X!ao_f
QQ: 120000512
Mail: xiao_f.mail#163.com
************************************/
var customToString = function(){
return '[' + this.Type.type + ' ' + this.Type.name + ']';
}
//
var createMethod = (function(){
//
var createMethodProxy = function(context, name){
// , ,
var method = function(){
// ,
if(!method.__initialized__){
initializeMethod(method);
}
//
var signature;
if(arguments.length){
var list = [];
for(var i=0; ivar typename;
var argument = arguments[i];
if(argument === undefined || argument === null){
typename = 'object';
}else if(argument instanceof Array){
typename = 'array';
}else if(argument instanceof Date){
typename = 'date';
}else{
typename = typeof argument;
if(typename == 'object'){
if('Class' in argument){
typename = argument.Class.Type.name;
}else if('nodeType' in argument){
typename = 'element';
}
}
}
list.push(typename);
}
signature = list.join(',');
}else{
signature = '';
}
// ,
if(method.__overloads__[signature]){
return method.__overloads__[signature].apply(this, arguments);
}else{
// ,
// ,
if(method.__overloadsCache__[signature]){
return method.__overloadsCache__[signature].apply(this, arguments);
}
//
for(var i=0; i// , ,
if(method.__overloadsRegExp__[i].regexp.test(signature)){
method.__overloadsCache__[signature] = method.__overloadsRegExp__[i].fn;
return method.__overloadsRegExp__[i].fn.apply(this, arguments);
}
}
// ,
if(method.__overloads__['default']){
return method.__overloads__['default'].apply(this, arguments);
}else if(method.__overloads__['']){
return method.__overloads__[''].apply(this, arguments);
}else{
alert('Error: '+method.Type.name+'('+signature+') is undefined.');
}
}
};
//
method.__context__ = context;
method.__functions__ = {};
method.toString = customToString;
//
method.Type = {
name: name,
Method: method,
type: 'method'
};
return method;
}
//
var initializeMethod = function(method){
//
method.__overloads__ = {};
//
method.__overloadsRegExp__ = [];
//
method.__overloadsCache__ = {};
//
for(var signature in method.__functions__){
var fn = method.__functions__[signature];
var params = signature.substring(signature.indexOf('(') + 1, signature.length - 1);
var pure = !/[\*\+\?\{]/.test(params);
//
if(pure){
method.__overloads__[params] = fn;
}else{
//
var regexp = '^' + params
.replace(/([\w\.]+)(\{.*?\})?/g, '($1(,|$))$2')
.replace(/\./g, '\\.')
.replace(/((\()var(\())/g, '$2\\w+$3')
.replace(/,\(/g, '(') + '$';
method.__overloadsRegExp__.push({ regexp: new RegExp(regexp), fn: fn });
}
}
method.__initialized__ = true;
}
//
return function(signature, fn, comp){
// ,
if(typeof signature == 'object'){
var context = {};
var method;
for(var key in signature){
method = createMethod.call(context, 'anonymous'+key, signature[key]);
}
return method;
}
signature = signature.replace(/\s+/g, '');
var index = signature.indexOf('(');
var name = index > -1 ? signature.substring(0, signature.indexOf('(')) : signature;
var context = this;
var method = context[name];
// ,
if(method === undefined){
context[name] = method = createMethodProxy(context, name);
}else if(!method.Type || method.Type.type!='method'){
// ,
var temp = method;
context[name] = method = createMethodProxy(context, name);
method.__functions__[name + '()'] = temp;
}else{
// , ,
// , false,
if(method.__context__ !== context){
var temp = method;
context[name] = method = createMethodProxy(context);
for(var sign in temp.__functions__){
method.__functions__[sign] = temp.__functions__[sign];
}
}else{
method.__initialized__ = false;
}
}
//
//
if(comp){
if(fn.__functions__){
for(var key in fn.__functions__){
if(key in method.__functions__){
method.__functions__[key].__overridden__ = fn;
}else{
method.__functions__[key] = fn;
}
}
}else{
if(signature in method.__functions__){
method.__functions__[signature].__overridden__ = fn;
}else{
method.__functions__[signature] = fn;
}
}
}else{
//
if(fn.__functions__){
for(var key in fn.__functions__){
if(key in method.__functions__){
fn.__functions__[key].__overridden__ = method;
}
method.__functions__[key] = fn.__functions__[key];
}
}else{
if(signature in method.__functions__){
fn.__overridden__ = method;
}
method.__functions__[signature] = fn;
}
}
if(this.Type && this.Type.type == 'package'){
return this;
}else{
return method;
}
};
})();
//
var createClass = (function(){
var slice = Array.prototype.slice;
var emptyFn = function(){};
var createClass = function(name){
return function(){
this[name].apply(this, slice.call(arguments, 0));
};
}
//
var baseCaller = function(){
if(arguments.length){
var args = slice.call(arguments, 0);
return baseCaller.caller.__overridden__.apply(this, args);
}else{
return baseCaller.caller.__overridden__.call(this);
}
}
//
var selfCaller = function(){
if(arguments.length){
var args = slice.call(arguments, 0);
return selfCaller.caller.__self__.apply(this, args);
}else{
return selfCaller.caller.__self__.call(this);
}
}
var filter = {prototype:true, Type:true};
//
function clone(a){
var fn = function(){};
fn.prototype = a;
return new fn;
}
// , ( )
function replace(base, self){
for(var key in self){
if(!(key in filter)){
if(typeof self[key] == 'function'){
//
if(key.indexOf('(') > -1 || (base[key] && base[key].__functions__)){
createMethod.call(base, key, self[key]);
}else{
//
if(key in base){
//
self[key].__overridden__ = base[key];
}
base[key] = self[key];
}
}else{
base[key] = self[key];
}
}
}
}
// , ( )
function complement(self, base){
for(var key in base){
if(!(key in filter)){
if(typeof base[key] == 'function'){
if(key.indexOf('(') > -1 || (self[key] && self[key].__functions__)){
createMethod.call(self, key, base[key], true);
}else{
if(key in self){
//
self[key].__overridden__ = base[key];
}else{
self[key] = base[key];
}
}
}else if(!(key in self)){
self[key] = base[key];
}
}
}
}
return function(){
//
if(this.Type && this.Type.type == 'package'){
if(arguments.length == 2){
var name = arguments[0];
var body = arguments[1];
}else{
var name = arguments[0];
var config = arguments[1];
var body = arguments[2];
}
}else{
if(arguments.length == 1){
var name = 'Anonymous';
var body = arguments[0];
}else{
var name = 'Anonymous';
var config = arguments[0];
var body = arguments[1];
}
}
//
var clazz = createClass(name);
//
var baseClass;
if(config && config.extend){
baseClass = config.extend;
}
// ,
if(typeof body == 'function'){
body = body(clazz);
}
//
if(body.Static){
complement(clazz, body.Static);
delete body.Static;
body = body.Public||body;
}else{
body = body.Public||body;
}
//
if(baseClass){
//
clazz.prototype = clone(baseClass.prototype);
//
complement(clazz, baseClass);
//
complement(clazz.prototype, body);
}else{
//
clazz.prototype = {};
complement(clazz.prototype, body);
}
//
if(config && config.mixin){
var mixin = config.mixin;
if(mixin instanceof Array){
for(var i=0; ireplace(clazz.prototype, mixin[i]);
}
}else{
replace(clazz.prototype, mixin);
}
}
//
clazz.prototype.base = baseCaller;
clazz.prototype.self = selfCaller;
clazz.prototype.constructor = clazz;
clazz.prototype.toString = customToString;
clazz.toString = customToString;
clazz.prototype.Class = clazz;
if(clazz.prototype[name]){
var constructor = clazz.prototype[name];
if(constructor.__functions__){
for(var key in constructor.__functions__){
// , , this.self
constructor.__functions__[key].__self__ = constructor;
// , ,
// base
if(baseClass){
constructor.__functions__[key].__overridden__ = baseClass.prototype[baseClass.Type.shortName];
}
}
}else if(baseClass){
clazz.prototype[name].__overridden__ = baseClass.prototype[baseClass.Type.shortName];
}
}else{
clazz.prototype[name] = emptyFn;
}
//
// ,
if(this.Type && this.Type.type == 'package'){
clazz.Type = {
type:'class',
name: this.Type.name+'.'+name,
shortName: name,
Package: this,
Class: clazz,
baseClass: baseClass
}
clazz.prototype.Type = {
type: 'object',
name: this.Type.name+'.'+name
}
//
this[name] = clazz;
//
if(name in clazz){
clazz[name].call(clazz);
}
// this
return this;
}else{
//
clazz.Type = {
type:'class',
name: name,
shortName: name,
Class: clazz,
baseClass: baseClass
}
clazz.prototype.Type = {
type: 'object',
name: name,
baseClass: baseClass
}
if(name in clazz){
clazz[name].call(clazz);
}
return clazz;
}
};
})();
//
var createObject = function(objects, config){
var target;
if(this.Type && this.Type.type == 'package'){
target = this;
}else{
target = {};
}
if(typeof objects == 'string'){
target = this[objects] = {};
objects = config;
}else if(typeof objects == 'function'){
objects = objects();
}
for(var key in objects){
if(typeof objects[key] == 'function' && (key.indexOf('(') > -1 || typeof target[key] == 'function')){
createMethod.call(target, key, objects[key]);
}else{
target[key] = objects[key];
}
}
if(this.Type && this.Type.type == 'package'){
return this;
}else{
return target;
}
};
//
var createPackage = (function(){
var root = this;
return function(package){
var name = [];
var path = package.split('.');
var parent = root;
for(var i=0; iname.push(path[i]);
if(parent[path[i]]){
parent = parent[path[i]];
}else{
var pack = {
Class: createClass,
Object: createObject,
Function: createMethod,
Package: createPackage,
toString: customToString
};
pack.Type = {
type: 'package',
Package: pack,
name: name.join('.')
}
parent = parent[path[i]] = pack;
}
}
return parent;
}
})();
// Package
window.Package = createPackage;
return {
Package: createPackage,
Class: createClass,
Function: createMethod,
Object: createObject
};
})();
終了語:
ここでlang.jsの応用と原理を紹介しました.このライブラリは主流ブラウザでテストに合格しました.
lang.jsを使いたいなら、ここで無料でダウンロードできます.何か問題があったら、いい提案があれば、フィードバックしてください.