オブジェクト指向の演算子

instanceofの:関数のオブジェクトインスタンスの特定のインスタンスかどうかを決定する
のinstanceof関数名、オブジェクト名:構文

 function Animal(){
        this.name = "join";
    }
    Animal.prototype.sex = "男";
    Animal.prototype.eat = function(){
        console.log("我正在吃火锅")
    }
    
    let animal = new Animal();
    animal.age = 19;
    animal.sex = "男";

    function Son(){
        Animal.call(this);
        this.a = 10;
        this.s = "s";
        this.fn = ()=>{
            console.log("你是个孩子")
        }
    }

    Son.prototype = deepCopy(Animal.prototype);
    Son.prototype.constructor = Son;
    
    var a1 = new Animal;
    var s1 = new Son();
    console.log(a1);   //Animal
    console.log(s1);   //Son
    console.log(s1 instanceof Son);    //true
    console.log(s1 instanceof Animal); //false

inPrototypeof() 限りプロトタイププロトタイプチェーンがあったとして、プロトタイプチェーンによって生成されるプロトタイプは、真を返すということができる
構文:プロトタイプ名.isPrototypeof(オブジェクト名)を
記述し、S1は、プロトタイプチェーンに沿って始まりますコールSon.prototypeがありますかどうかを確認するために、層ごとに見上げます

console.log(Son.prototype.isPrototypeOf(s1));

hasOwnPropertyを() この属性は、現在のオブジェクトかどうかを検出するために使用され、ブール戻り値は
唯一のコンストラクタ内のコンテンツを見つけるために、プロトタイプの内容とプロトタイプチェーンをチェックしません。
構文:オブジェクト名.hasOwnPoperty(「属性/メソッド名」);

console.log(s1.hasOwnProperty("fn"))

[削除]:プロパティを削除します

delete s1.name;

チェックオブジェクトが指定された属性が含まれているかどうかを
この属性は、オブジェクトの属性を直接することができ、それはまた、プロトタイプ継承されたプロパティをすることができます(
しかし、彼らのprototypeプロパティ/メソッドは、(プロトタイプ継承チェーン、再構築されたプロトタイプ、上書き)は動作しません)
構文:オブジェクト名内のプロパティ/メソッド名

 console.log('a' in s1);
    console.log('name' in s1);
    console.log('eat' in s1);
    console.log('score' in s1);
    console.log(s1.score);
公開された55元の記事 ウォンの賞賛1 ビュー1022

おすすめ

転載: blog.csdn.net/weixin_46191548/article/details/104220962