isPrototypeOf、instanceof、hasOwnProperty関数の概要



Introduction Isprototypeof



コレクションメンバーのメンバーシップを検出し、属性がオブジェクトに存在するかどうかを判断します。これは、in演算子hasOwnProperty()を使用して実行できます。

in演算子の左側はプロパティの名前であり、右側は文字列です。これは、オブジェクトの空き属性または継承属性にこの属性が含まれている場合に返されます。true
object hasOwnProperty()このメソッドは、指定された名前がオブジェクトの空きプロパティであるかどうかを検出するために使用され、継承されたプロパティである場合は、return false



Function Animal(){}//Define the Animal constructor Animal.prototype = {//Define the Animal prototype Species: 'animal', say:function(){ console.log('i can say word') } } Function Cat(name,color){//Define the constructor Cat this.name = name this.color = color } var F = function(){} F.prototype = Animal.prototype Cat.prototype = new F() Cat.prototype.constructor = Cat//Cat inherits Animal with F empty object as medium Var eh = new Cat('lili','white')//instantiate the object console.log('say' in eh)//=>true console.log('name' in eh)//=>true console.log('color' in eh)//=>true console.log('species' in eh)=>true Console.log(eh.hasOwnProperty('say'))=>false Since say is an inherited attribute non-own attribute Console.log(eh.hasOwnProperty('species'))=>false Since species is an inherited attribute non-own attribute console.log(eh.hasOwnProperty('name'))=>true console.log(eh.hasOwnProperty('color'))=>true for(var key in eh){ console.log(key) if(eh.hasOwnProperty(key)){ console.log(key) //=>species say name color } }

参考資料

Javascriptオブジェクト指向プログラミング(2):コンストラクターの継承
[JavaScriptオーソリティガイド]

個人ブログ:
攻撃手順