Skip to content

Instantly share code, notes, and snippets.

@kokudori
Created November 18, 2011 04:05
Show Gist options
  • Save kokudori/1375570 to your computer and use it in GitHub Desktop.
Save kokudori/1375570 to your computer and use it in GitHub Desktop.
JavaScriptのプロパティ解決の擬似コード
Object.prototype.send = function (name) {
return (function (prototype) {
if (!prototype)
return undefined;
if (name in prototype)
return prototype[name];
arguments.callee(prototype.__proto__);
})(this);
};
var Hoge = function () { this.aaa = 100; };
Hoge.prototype.hoge = "there are a Hoge's prototype";
var Piyo = function () { this.bbb = 200; };
Piyo.prototype = Hoge.prototype;
Piyo.prototype.piyo = "there are a Piyo's prototype";
var obj = new Piyo();
console.log(obj.send('hoge'));
console.log(obj.hoge);
/* 実行結果
there are a Hoge's prototype
there are a Hoge's prototype
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment