ここでは、オブジェクト引数を取り、渡されたオブジェクトをプロトタイプ オブジェクトとして、新しく作成されたオブジェクトを返す単純な継承関数があります。
function inherit(p) {
if (p == null) throw TypeError(); //p must not be null
if(Object.create) { //if the object.create method is defined
return Object.create(p); //use it
}
//type checking
var typeIs = typeof p; //variable that holds type of the object passed
if(typeIs !== 'object' && typeIs !== 'function') {
throw TypeError();
}
function f() {}; //dummy constructor
f.prototype = p; //prototype
return new f(); //return new constructor
}
var $f0 = {};
$f0.x = 1;
var $g0 = inherit($f0);
$g0.y = 2;
var $h0 = inherit($g0);
console.log('x' in $h0); //true
console.log(Object.getOwnPropertyNames($h0.prototype)); //throws error
私が抱えている問題は、inherit
関数を実行した後、オブジェクトのプロトタイプ プロパティを検索できないことです。
プロトタイプ オブジェクトのプロパティを表示するにはどうすればよいですか?