-2

以下の出力を理解しようとしています-オブジェクトで直接使用するとチェックがfalseになるのに、インスタンスでチェックするとtrueになるのはなぜですか?? 誰かが説明できますか - ここで何かが欠けていますか?

    function Book2(){
    this.title =  "High Performance JavaScript";
    this.publisher = "Yahoo! Press";
};

Book2.prototype.author = "hgghghg";

var b = new Book2();

alert(Book2.hasOwnProperty("title"));  //false
alert(Book2.hasOwnProperty("toString"));  //false
alert("title" in Book2); //false
alert("toString" in Book2); //true


alert(b.hasOwnProperty("title"));  //true
alert(b.hasOwnProperty("toString"));  //false
alert("title" in b); //true 
alert("toString" in b); //true
4

2 に答える 2

0

hasOwnPropertyプロトタイプチェーンを見ていない、inオペレーターが見ている

また、Bookは関数であり、独自のプロパティを持たず、applyや などのメソッドを継承しますcallBookwithのインスタンスをnew作成すると、プロトタイプ チェーンが で始まるオブジェクトが作成されるため、 のBook.prototypeようなプロパティが表示されますtitle

于 2016-02-08T13:16:36.450 に答える