Last active
December 16, 2015 13:39
-
-
Save DimitarChristoff/5443449 to your computer and use it in GitHub Desktop.
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
var prime = (function(){ | |
'use strict'; | |
var sFunction = 'function'; | |
var has = function(self, key){ | |
return Object.hasOwnProperty.call(self, key); | |
}; | |
var each = function(object, method, context){ | |
for (var key in object) | |
if (method.call(context, object[key], key, object) === false) | |
break; | |
return object; | |
}; | |
if (!{ valueOf: 0 }.propertyIsEnumerable('valueOf')){ | |
var buggy = 'constructor,toString,valueOf,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString'.split(','); | |
var proto = Object.prototype; | |
each = function(object, method, context){ | |
for (var key in object) | |
if (method.call(context, object[key], key, object) === false) | |
return object; | |
for (var i = 0; key = buggy[i]; i++){ | |
var value = object[key]; | |
if ((value !== proto[key] || has(object, key)) && method.call(context, value, key, object) === false) | |
break; | |
} | |
return object; | |
}; | |
} | |
var create = Object.create || function(self){ | |
var constructor = function(){ | |
}; | |
constructor.prototype = self; | |
return new constructor(); | |
}; | |
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; | |
var define = Object.defineProperty; | |
try { | |
var obj = { a: 1 }; | |
getOwnPropertyDescriptor(obj, 'a'); | |
define(obj, 'a', { value: 2 }); | |
} catch (e) { | |
getOwnPropertyDescriptor = function(object, key){ | |
return { value: object[key] }; | |
}; | |
define = function(object, key, descriptor){ | |
object[key] = descriptor.value; | |
return object; | |
}; | |
} | |
var implement = function(proto){ | |
each(proto, function(value, key){ | |
if (key !== 'constructor' && key !== 'define' && key !== 'inherits') | |
this.define(key, getOwnPropertyDescriptor(proto, key) || { | |
writable: true, | |
enumerable: true, | |
configurable: true, | |
value: value | |
}); | |
}, this); | |
return this; | |
}; | |
var prime = function(proto){ | |
var superprime = proto.inherits; | |
var constructor = has(proto, 'constructor') ? proto.constructor : superprime ? function(){ | |
return superprime.apply(this, arguments); | |
} : function(){ | |
}; | |
if (superprime){ | |
var superproto = superprime.prototype; | |
var cproto = constructor.prototype = create(superproto); | |
constructor.parent = superproto; | |
cproto.constructor = constructor; | |
} | |
constructor.define = proto.define || superprime && superprime.define || function(key, descriptor){ | |
define(this.prototype, key, descriptor); | |
return this; | |
}; | |
constructor.implement = implement; | |
return constructor.implement(proto); | |
}; | |
prime.has = has; | |
prime.each = each; | |
prime.create = create; | |
prime.define = define; | |
prime.options = prime({ | |
setOptions: function(options){ | |
var args = [{}, this.options], | |
option, | |
o; | |
args.push.apply(args, arguments); | |
o = this.options = _.extend.apply(null, args); | |
// add the events as well | |
for(option in o){ | |
if (o.hasOwnProperty(option)){ | |
if (typeof o[option] != sFunction || !(/^on[A-Z]/).test(option)) continue; | |
this.on(removeOn(option), o[option]); | |
delete o[option]; | |
} | |
} | |
return this; | |
} | |
}); | |
prime.parent = prime({ | |
parent: function(method){ | |
var parent = this._parent || this.constructor.parent, | |
result; | |
this._parent = parent.constructor.parent; | |
result = parent[method].apply(this, slice.call(arguments, 1)); | |
this._parent = parent; | |
return result; | |
} | |
}) | |
return prime; | |
}()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment