Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created November 9, 2011 20:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/1352801 to your computer and use it in GitHub Desktop.
Save Raynos/1352801 to your computer and use it in GitHub Desktop.
OO utilities
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);
}
var beget = function (proto) {
var args = [].slice.call(arguments, 1);
var o = Object.create(proto);
o.constructor.apply(o, args);
return o;
}
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;
}
});
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