Permalink
Browse files

Reformat

  • Loading branch information...
1 parent 2d27cf1 commit c86838a38de7b82719f08ac29bd40c8633dd4039 @tnhu committed Apr 29, 2012
Showing with 70 additions and 43 deletions.
  1. +70 −43 jsface.js
View
113 jsface.js
@@ -10,50 +10,69 @@
/**
* Check an object is a map or not. A map is something like { key1: value1, key2: value2 }.
+ * @param obj object to be checked
+ * @return true if object is a map
*/
- function isMap(obj) {
- return (obj && typeof obj === OBJECT && !(typeof obj.length === NUMBER && !(obj.propertyIsEnumerable(LENGTH))));
- }
+ function isMap(obj) { return (obj && typeof obj === OBJECT && !(typeof obj.length === NUMBER && !(obj.propertyIsEnumerable(LENGTH)))); }
/**
* Check an object is an array or not. An array is something like [].
+ * @param obj object to be checked
+ * @return true if object is an array
*/
- function isArray(obj) {
- return (obj && typeof obj === OBJECT && typeof obj.length === NUMBER && !(obj.propertyIsEnumerable(LENGTH)));
- }
+ function isArray(obj) { return (obj && typeof obj === OBJECT && typeof obj.length === NUMBER && !(obj.propertyIsEnumerable(LENGTH))); }
/**
* Check an object is a function or not.
+ * @param obj object to be checked
+ * @return true if object is an function
*/
- function isFunction(obj) {
- return (obj && typeof obj === "function");
- }
+ function isFunction(obj) { return (obj && typeof obj === "function"); }
/**
* Check an object is a string not.
+ * @param obj object to be checked
+ * @return true if object is a function
*/
- function isString(obj) {
- return toString.apply(obj) === "[object String]";
- }
+ function isFunction(obj) { return (obj && typeof obj === "function"); }
+
+ /**
+ * Check an object is a string not.
+ * @param obj object to be checked
+ * @return true if object is a string
+ */
+ function isString(obj) { return toString.apply(obj) === "[object String]"; }
/**
* Check an object is a class (not an instance of a class, which is a map) or not.
+ * @param obj object to be checked
+ * @return true if object is a class
+ */
+ function isClass(obj) { return isFunction(obj) && (obj.prototype && obj === obj.prototype.constructor); }
+
+ /**
+ * Util for extend() to copy a map of { key:value } to an object
+ * @param key key
+ * @param value value
+ * @param ignoredKeys ignored keys
+ * @param object object
+ * @param iClass true if object is a class
+ * @param oPrototype object prototype
*/
- function isClass(clazz) {
- return isFunction(clazz) && (clazz.prototype && clazz === clazz.prototype.constructor);
+ function copier(key, value, ignoredKeys, object, iClass, oPrototype) {
+ if ( !ignoredKeys || !ignoredKeys.hasOwnProperty(key)) {
+ object[key] = value;
+ if (iClass) { oPrototype[key] = value; } // class? copy to prototype as well
+ }
}
/**
* Extend object from subject, ignore properties in ignoredKeys
+ * @param object the child
+ * @param subject the parent
+ * @param ignoredKeys (optional) keys should not be copied to child
*/
function extend(object, subject, ignoredKeys) {
- function copier(key, value) {
- if ( !ignoredKeys || !ignoredKeys.hasOwnProperty(key)) { // no copy ignored keys
- object[key] = value; // do copy
- if (iClass) { oPrototype[key] = value; } // class? copy to prototype as well
- }
- }
-
if (isArray(subject)) {
for (var len = subject.length; --len >= 0;) { extend(object, subject[len], ignoredKeys); }
} else {
@@ -64,11 +83,11 @@
oPrototype = object.prototype, supez, key, proto;
// copy static properties and prototype.* to object
- if (isMap(subject)) { for (key in subject) copier(key, subject[key]); }
+ if (isMap(subject)) { for (key in subject) copier(key, subject[key], ignoredKeys, object, iClass, oPrototype); }
if (isSubClass) {
proto = subject.prototype;
- for (key in proto) { copier(key, proto[key]); }
+ for (key in proto) { copier(key, proto[key], ignoredKeys, object, iClass, oPrototype); }
}
// prototype properties
@@ -78,26 +97,36 @@
/**
* Create a class.
+ * @param parent parent class(es)
+ * @param api class api
+ * @return class
*/
function Class(parent, api) {
- if ( !api) parent = (api = parent, 0);
+ if ( !api) parent = (api = parent, 0); // !api means there's no parent
var clazz, constructor, singleton, statics, key, bindTo, len, i = 0, p,
ignoredKeys = { constructor: 1, $singleton: 1, $statics: 1, prototype: 1, $super: 1, $superp: 1, main: 1 },
overload = Class.overload,
plugins = Class.plugins;
- api = (typeof api === "function" ? api() : api) || {}; // execute api if it's a function
- constructor = api.hasOwnProperty("constructor") ? api.constructor : 0; // hasOwnProperty is a must, constructor is special
+ api = (typeof api === "function" ? api() : api) || {}; // execute api if it's a function
+ constructor = api.hasOwnProperty("constructor") ? api.constructor : 0; // hasOwnProperty is a must, constructor is special
singleton = api.$singleton;
statics = api.$statics;
- for (key in plugins) { ignoredKeys[key] = 1; } // add plugins' keys into ignoredKeys
+ // add plugins' keys into ignoredKeys
+ for (key in plugins) { ignoredKeys[key] = 1; }
+ // construct constructor
clazz = singleton ? {} : (constructor ? (overload ? overload("constructor", constructor) : constructor) : function(){});
+
+ // determine bindTo: where api should be bound
bindTo = singleton ? clazz : clazz.prototype;
+ // make sure parent is always an array
parent = !parent || isArray(parent) ? parent : [ parent ];
+
+ // do inheriting
len = parent && parent.length;
while (i < len) {
p = parent[i++];
@@ -107,27 +136,24 @@
if ( !singleton) { clazz[key] = p[key]; }
}
}
- for (key in p.prototype) {
- if ( !ignoredKeys[key]) { bindTo[key] = p.prototype[key]; }
- }
+ for (key in p.prototype) { if ( !ignoredKeys[key]) { bindTo[key] = p.prototype[key]; } }
}
- for (key in api) {
- if ( !ignoredKeys[key]) bindTo[key] = api[key];
- }
- for (key in statics) {
- clazz[key] = bindTo[key] = statics[key];
- }
+ // copy properties from api to bindTo
+ for (key in api) { if ( !ignoredKeys[key]) bindTo[key] = api[key]; }
+ // copy static properties from statics to both clazz and bindTo
+ for (key in statics) { clazz[key] = bindTo[key] = statics[key]; }
+
+ // if class is not a singleton, add $super and $superp
if ( !singleton) {
p = parent && parent[0] || parent;
clazz.$super = p;
clazz.$superp = p && p.prototype ? p.prototype : p;
}
- for (key in plugins) { plugins[key](clazz, parent, api); } // pass control to plugins
- if (isFunction(api.main)) { api.main.call(clazz, clazz); } // execute main()
-
+ for (key in plugins) { plugins[key](clazz, parent, api); } // pass control to plugins
+ if (isFunction(api.main)) { api.main.call(clazz, clazz); } // execute main()
return clazz;
}
@@ -136,6 +162,7 @@
/* Initialization */
jsface = {
+ version : "2.1.1",
Class : Class,
extend : extend,
isMap : isMap,
@@ -145,12 +172,12 @@
isClass : isClass
};
- if (typeof module !== "undefined" && module.exports) { // NodeJS/CommonJS
+ if (typeof module !== "undefined" && module.exports) { // NodeJS/CommonJS
module.exports = jsface;
} else {
- oldClass = context.Class; // save current Class namespace
- context.Class = Class; // bind Class and jsface to global scope
+ oldClass = context.Class; // save current Class namespace
+ context.Class = Class; // bind Class and jsface to global scope
context.jsface = jsface;
- jsface.noConflict = function() { context.Class = oldClass; } // no conflict
+ jsface.noConflict = function() { context.Class = oldClass; } // no conflict
}
})(this, "object", "number", "length", Object.prototype.toString);

0 comments on commit c86838a

Please sign in to comment.