Last active
December 20, 2015 18:08
-
-
Save lylijincheng/6173135 to your computer and use it in GitHub Desktop.
klass
This file contains 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
// Klass | |
(function(exports) { | |
// shim | |
if (typeof Object.create !== "function") { | |
Object.create = function(o) { | |
function F() {} | |
F.prototype = o; | |
return new F; | |
}; | |
} | |
// Utility `proxy` | |
var proxy = function(fn, scope) { | |
var context = scope || this; | |
return fn.bind ? fn.bind(context) : function() { | |
return fn.apply(context, arguments); | |
} | |
}; | |
// Utility `extend` | |
var extend = function(dest, source) { | |
var i; | |
for (i in source) { | |
if (source.hasOwnProperty(i)) { | |
dest[i] = source[i]; | |
} | |
} | |
}; | |
/** | |
* 类对象原型,原始对象。 | |
*/ | |
var klass = { | |
// initialize: function() {}, | |
/** | |
* 类对象的 `prototype`,包含初始化方法。 | |
*/ | |
fn: { | |
initialize: function() {} | |
}, | |
/** | |
* 创建一个类对象 | |
* | |
* @param {Object} props | |
* @param {Object} statics | |
* @return new klass object | |
*/ | |
create: function(props, statics) { | |
var object = Object.create(this); | |
// object.initialize.apply(object, arguments); | |
object.fn = Object.create(this.fn); | |
object.parent = this; | |
if (props) { | |
this.implement.call(object, props); | |
} | |
if (statics) { | |
this.extend.call(object, statics); | |
} | |
return object; | |
}, | |
/** | |
* 初始化构造函数,产生类对象的一个实例。 | |
* 对于构造函数,相当于调用 `new Constructor` | |
* | |
* @params {Mixed} | |
* @link fn.initialize | |
* @return new instance. | |
*/ | |
instance: function() { | |
var instance = Object.create(this.fn); | |
instance.parent = this; | |
instance.initialize.apply(instance, arguments); | |
return instance; | |
}, | |
/** | |
* 扩展类对象的静态方法 | |
* | |
* @param {Object} statics | |
* @return `this` | |
*/ | |
extend: function(statics) { | |
var extended = statics.extended; | |
extend(this, statics); | |
if (extended) { | |
extended.apply(this); | |
} | |
return this; | |
}, | |
/** | |
* 扩展类对象的实例方法 | |
* | |
* @param {Object} props | |
* @return `this` | |
*/ | |
implement: function(props) { | |
var included = props.included; | |
extend(this.fn, props); | |
if (included) { | |
included.apply(this); | |
} | |
return this; | |
} | |
}; | |
klass.proxy = klass.fn.proxy = proxy; | |
exports.klass = klass; | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment