Created
June 30, 2012 18:13
-
-
Save ukyo/3024909 to your computer and use it in GitHub Desktop.
hshs.js is a simple oop library
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
/* | |
USAGE | |
web: | |
<script>var __scope__ = yournamespace;</script> | |
<script src="hshs.js"></script> | |
node: | |
var hshs = require('path/to/hshs.js'); | |
EXAMPLE | |
hshs.cls.extend(function A () { | |
this.init = function (name) { | |
this.name = name; | |
}; | |
this.hello = function () { | |
console.log(this.name + ': hello!'); | |
}; | |
this.hai = function () { | |
console.log(this.name + ': hai!'); | |
}; | |
}); | |
hshs.cls.extend.A(function B (sup) { | |
this.init = function () { | |
sup.init.call(this, 'b'); | |
}; | |
this.hello = function () { | |
sup.hello.call(this); | |
console.log(this.name + ': hello!!!'); | |
}; | |
}); | |
hshs.cls.extend.B(function C (sup) { | |
this.init = function (a, b) { | |
this.name = a; | |
this.type = b; | |
}; | |
this.hello = function () { | |
sup.hello.call(this); | |
console.log(this.name + ': hello!!!!!'); | |
console.log(this.type); | |
}; | |
}); | |
var b = new hshs.cls.B(); | |
b.hello(); | |
b.hai(); | |
var c = new hshs.cls.C('hasuta', 'syota'); | |
c.hello(); | |
c.hai(); | |
*/ | |
(function (hshs) { | |
hshs.cls = {}; | |
hshs.cls.extend = function (parent, F) { | |
var child, cp, pp, f; | |
if (arguments.length === 1) { | |
F = parent; | |
parent = hshs.cls.Base; | |
} | |
parent = parent || hshs.cls.Base; | |
F = F || function () {}; | |
pp = parent.prototype; | |
f = new F(pp); | |
child = f.init || function () { pp.init.apply(this, arguments); }; | |
cp = child.prototype = Object.create(pp); | |
Object.keys(f).forEach(function (key) { cp[key] = f[key]; }); | |
Object.defineProperty(cp, 'constructor', { | |
value: child, | |
writable: true, | |
enumerable: false, | |
configurable: true | |
}); | |
if (F.name && F.name !== 'Base') { | |
hshs.cls[F.name] = child; | |
hshs.cls.extend[F.name] = function (F) { return hshs.cls.extend(child, F); }; | |
} | |
return child; | |
}; | |
hshs.cls.Base = function Base () {}; | |
hshs.cls.extend.Base = function (F) { return hshs.cls.extend(hshs.cls.Base, F); }; | |
hshs.cls.Base.prototype.init = hshs.cls.Base; | |
hshs.util = {}; | |
hshs.util.copy = function (to, from) { | |
Object.getOwnPropertyNames(from).forEach(function (key) { | |
to[key] = from[key]; | |
}); | |
}; | |
hshs.util.clone = function (from) { | |
if (Array.isArray(from)) { | |
return from.map(hshs.util.clone); | |
} else if (typeof from === 'function') { | |
return from; | |
} else if (typeof from === 'object' && from !== null) { | |
var o = {}; | |
Object.getOwnPropertyNames(from).forEach(function (key) { | |
o[key] = hshs.util.clone(from[key]); | |
}); | |
return o; | |
} else { | |
return from; | |
} | |
}; | |
hshs.trait = {}; | |
hshs.trait.include = function (object, traits, scope) { | |
scope = scope || hshs.trait; | |
if (typeof modules === 'string') { | |
traits = traits.split(/\s+/).map(function (key) { | |
return scope[key]; | |
}); | |
} | |
modules.forEach(function (module) { | |
hshs.util.copy(object, module); | |
}); | |
}; | |
})(function () { | |
var global; | |
if (typeof module !== 'undefined' && module.exports) { | |
global = module.exports; | |
} else if (typeof __scope__ !== 'undefined'){ | |
global = __scope__; | |
} else { | |
global = this; | |
} | |
return global; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment