Skip to content
Myr

oo.js

This file

Path

lib/support/oo.js

83 lines · 1.9 KiB · JavaScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 5342d04 point at it.

Docsvisionmediabefore adoption5342d04

lines 1–83why, one step back
1
2// OO - Class - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3// Based on http://ejohn.org/blog/simple-javascript-inheritance/4// which is based on implementations by Prototype / base256;(function(){7 var global = this, initialize = true8 var referencesSuper = /xyz/.test(function(){ xyz }) ? /\b__super__\b/ : /.*/910 /**11 * Shortcut for Class.extend()12 *13 * @param {hash} props14 * @return {function}15 * @api public16 */1718 Class = function(props){19 if (this == global)20 return Class.extend(props) 21 }22 23 // --- Version24 25 Class.version = '1.0.0'26 27 /**28 * Create a new class.29 *30 * User = Class({31 * init: function(name){32 * this.name = name33 * }34 * })35 *36 * Classes may be subclassed using the .extend() method, and37 * the associated superclass method via this.__super__().38 *39 * Admin = User.extend({40 * init: function(name, password) {41 * this.__super__(name)42 * // or this.__super__.apply(this, arguments)43 * this.password = password44 * }45 * })46 *47 * @param {hash} props48 * @return {function}49 * @api public50 */51 52 Class.extend = function(props) {53 var __super__ = this.prototype54 55 initialize = false56 var prototype = new this57 initialize = true5859 for (var name in props)60 prototype[name] = 61 typeof props[name] == 'function' &&62 typeof __super__[name] == 'function' &&63 referencesSuper.test(props[name]) ?64 (function(name, fn){65 return function() {66 this.__super__ = __super__[name]67 return fn.apply(this, arguments)68 }69 })(name, props[name])70 : props[name]71 72 function Class() {73 if (initialize && this.init)74 this.init.apply(this, arguments)75 }76 77 Class.prototype = prototype78 Class.constructor = Class79 Class.extend = arguments.callee80 81 return Class82 }83})()