Skip to content

Instantly share code, notes, and snippets.

@mapserver2007
Created May 17, 2011 05:16
Show Gist options
  • Save mapserver2007/975990 to your computer and use it in GitHub Desktop.
Save mapserver2007/975990 to your computer and use it in GitHub Desktop.
プロトタイプベースOOP試作その2.1
var Module = {};
Module.create = function(base) {
if (!!document.attachEvent) {
base.mix = function() {
var parents = arguments,
child = this;
for (var i = 0, len = parents.length; i < len; i++) {
var parent = parents[i];
for (var prop in parent) if (!child.hasOwnProperty(prop)) {
if (isReserved(prop)) throw "reserved property";
child[prop] = parent[prop];
}
}
return child;
};
}
else {
base.parent = function() { return this.__proto__; };
base.mix = function() {
var clone = function(o) {
var c = {};
for (var prop in o) if (!isReserved(prop)) {
c[prop] = o[prop];
}
c.__proto__ = o.__proto__;
return c;
};
var isReserved = function(value) {
var reservedList = ["mix", "parent"];
for (var i = 0; i < reservedList.length; i++) {
if (reservedList[i] === value) {
return true;
}
}
return false;
};
var parents = arguments,
child = clone(this);
for (var i = 0, len = parents.length; i < len; i++) {
var parent = {};
var depth = 0, _c = child;
for (var prop in parents[i]) {
parent[prop] = parents[i][prop];
}
for (;;) {
_c = _c.__proto__;
if (_c !== null) {
depth++;
}
else {
break;
}
}
var plist = ["child"];
for (var d = 1; d <= depth; d++) {
plist[d] = "__proto__";
};
eval(plist.join(".") + " = parent");
}
return child;
};
}
return base;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment