2021年2月15日月曜日

javascript internal slot [[Prototype]]について

 Internal methods and internal slots are identified within this specification using names enclosed in double square brackets [[ ]].(© Ecma International 2020)

[[ ]]は、ECMAScriptでの表記方法

普通のオブジェクト(ordinary object)は、[[Prototype]]を持っています。

[[Prototype]]は、nullまたは、他のオブジェクトの参照を持っています。


普通にオブジェクトを作成すると、[[Prototype]]は、Object.protorypeの参照を持ちます。

let obj1 = {};
let obj2 = {};
let obj3 = Object.create(obj1);   // [[Protorype]]は、obj1

Object.prototype.my_prop = "AAA"

// すべてAAAが表示される。
alert(obj1.my_prop);
alert(obj2.my_prop);
alert(obj3.my_prop);

alert("abc".my_prop);  // 文字列も[[Protorype]]を持っており、最終的にObject.prototypetが参照される。


0 件のコメント:

コメントを投稿