Created
November 9, 2011 20:07
-
-
Save Raynos/1352801 to your computer and use it in GitHub Desktop.
OO utilities
This file contains hidden or 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
if (typeof(Name) === 'undefined') { | |
(function(global) { | |
"use strict"; | |
function defineNamespace(object, namespace) { | |
var privates = Object.create(object), | |
base = object.valueOf; | |
Object.defineProperty(object, 'valueOf', { | |
value: function valueOf(value) { | |
if (value !== namespace || this !== object) { | |
return base.apply(this, arguments); | |
} else { | |
return privates; | |
} | |
}); | |
return privates; | |
} | |
function Name() { | |
var namespace = {}; | |
return function name(object) { | |
var privates = object.valueOf(namespace); | |
return privates !== object ? privates : defineNamespace(object, namespace); | |
}; | |
} | |
return global.Name = Name; | |
})(this); | |
} |
This file contains hidden or 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 beget = function (proto) { | |
var args = [].slice.call(arguments, 1); | |
var o = Object.create(proto); | |
o.constructor.apply(o, args); | |
return o; | |
} |
This file contains hidden or 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 SomeKlass = (function () { | |
var privates = Name(); | |
return { | |
public_method: public_method, | |
public_prop: 42, | |
constructor: constructor | |
}; | |
function public_method() { | |
return local_utility(); | |
} | |
function constructor() { | |
privates(this).secret = secret; | |
} | |
function local_utility(_) { | |
return privates(this).secret; | |
} | |
}); |
This file contains hidden or 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 instance = beget(SomeKlass, 42); | |
console.log(instance.public_method() === instance.public_prop); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment