oo.js
This file
Path
lib/support/oo.js
93 lines · 2.3 KiB · JavaScript
Last changed by
Updated js-oo to 1.1.0 for mixins etcvisionmediabefore adoption37948d8
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
// OO - Class - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)// Based on http://ejohn.org/blog/simple-javascript-inheritance/// which is based on implementations by Prototype / base2
;(function(){ var global = this, initialize = true var referencesSuper = /xyz/.test(function(){ xyz }) ? /\b__super__\b/ : /.*/
/** * Shortcut for Class.extend() * * @param {hash} props * @return {function} * @api public */
Class = function(props){ if (this == global) return Class.extend(props) } // --- Version Class.version = '1.1.0' /** * Create a new class. * * User = Class({ * init: function(name){ * this.name = name * } * }) * * Classes may be subclassed using the .extend() method, and * the associated superclass method via this.__super__(). * * Admin = User.extend({ * init: function(name, password) { * this.__super__(name) * // or this.__super__.apply(this, arguments) * this.password = password * } * }) * * @param {hash} props * @return {function} * @api public */ Class.extend = function(props) { var __super__ = this.prototype initialize = false var prototype = new this initialize = true
function Class() { if (initialize && this.init) this.init.apply(this, arguments) } Class.include = function(props) { for (var name in props) if (name == 'include') if (props[name] instanceof Array) for (var i = 0, len = props[name].length; i < len; ++i) Class.include(props[name][i]) else Class.include(props[name]) else if (props.hasOwnProperty(name)) prototype[name] = typeof props[name] == 'function' && typeof __super__[name] == 'function' && referencesSuper.test(props[name]) ? (function(name, fn){ return function() { this.__super__ = __super__[name] return fn.apply(this, arguments) } })(name, props[name]) : props[name] } Class.include(props) Class.prototype = prototype Class.constructor = Class Class.extend = arguments.callee return Class }})()