他のデータ型と同様に、 オブジェクトは組み込みの Object プロトタイプからプロパティとメソッドを継承しますが、 つまり、結果のオブジェクトには、定義したプロパティと プロトタイプから継承されたメソッドを含むプロンプター プロパティ:
let myObject = { 'booleanValue' : true }; myObject; > Object { booleanValue: true } booleanValue: true [[prototype]]: Object { … } __defineGetter__: function __defineGetter__() __defineSetter__: function __defineSetter__() __lookupGetter__: function __lookupGetter__() __lookupSetter__: function __lookupSetter__() __proto__: … constructor: function Object() hasOwnProperty: function hasOwnProperty() isPrototypeOf: function isPrototypeOf() propertyIsEnumerable: function propertyIsEnumerable() toLocaleString: function toLocaleString() toString: function toString() valueOf: function valueOf() <get __proto__()>: function __proto__() <set __proto__()>: function __proto__() プロトタイプ プロパティは、プロパティキーで直接アクセスするためのものではありません。として 上記の例では、これは [[prototype]] によって暗黙的に指定されています。 または <prototype> 表記がブラウザで使用されているデベロッパー コンソール、Google Cloud の 次のドキュメントを参照してください。
// Chrome: let emptyObject = {}; emptyObject; > {} [[prototype]]: Object // Firefox: let emptyObject = {}; emptyObject; > Object { } <prototype>: Object { … } すべての一般的なブラウザでは __proto__ を事実上の標準として使用していますが、これは事実ではありません。 本番環境のコードでは使用しないでください。
let emptyObject = {}; emptyObject.__proto__; > Object { … } __defineGetter__: function __defineGetter__() __defineSetter__: function __defineSetter__() __lookupGetter__: function __lookupGetter__() __lookupSetter__: function __lookupSetter__() __proto__: constructor: function Object() hasOwnProperty: function hasOwnProperty() isPrototypeOf: function isPrototypeOf() propertyIsEnumerable: function propertyIsEnumerable() toLocaleString: function toLocaleString() toString: function toString() valueOf: function valueOf() <get __proto__()>: function __proto__() <set __proto__()>: function __proto__() 代わりに、オブジェクトの [[Prototype]] に直接アクセスして変更することができます。 組み込みの Object.getPrototypeOf() と Object.setPrototypeOf() を使用 メソッド:
let myObj = { "value" : 5 }; let protoParent = { "protoValue" : true }; myObj; Object { value: 5 } value: 5 <prototype>: Object { … } Object.getPrototypeOf( myObj ); > Object { … } __defineGetter__: function __defineGetter__() __defineSetter__: function __defineSetter__() __lookupGetter__: function __lookupGetter__() __lookupSetter__: function __lookupSetter__() __proto__: constructor: function Object() hasOwnProperty: function hasOwnProperty() isPrototypeOf: function isPrototypeOf() propertyIsEnumerable: function propertyIsEnumerable() toLocaleString: function toLocaleString() toString: function toString() valueOf: function valueOf() <get __proto__()>: function __proto__() <set __proto__()>: function __proto__() Object.setPrototypeOf( myObj, protoParent ); > Object { value: 5 } value: 5 <prototype>: Object { protoValue: true } 継承されたプロパティと作成者が定義したプロパティを区別するには、 後者は通常、オブジェクトの「独自のプロパティ」と呼ばれます。
組み込みの Object.hasOwn() メソッドは true を返します(指定されたプロパティが オブジェクトの直接プロパティ。プロパティが継承または継承されている場合は false。 ありません。可能な限り、Object.hasOwn() を使用してください。 代わりに、継承された hasOwnProperty() メソッドを Object.create()。
let myObject = { 'myValue' : 100 }; Object.hasOwn( myObject, 'myValue' ); > true myObject.__proto__; // The Object prototype inherited by `myObject` is present: > Object { … } Object.hasOwn( myObject, '__proto__' ); // The Object prototype inherited by `myObject` is not an "own property:" > false 理解度をチェックする
__proto__ の使用を避けるべき理由は何ですか。