JavaScript based learning - and shielding property settings
Property of the object
- Modify the properties of the object itself
var myObject = { a: 2 };
myObject.a = 3;
console.log(myObject); // {a: 3}
- If A is not a direct presence in myObject, [[the Prototype]] chain will be traversed, if not found in the original chain, it will be added directly to a myObject
myObject.b =. 3, b is not found in the myObject, to anotherObject look on, can not find, Object object has been found, did not find b, b add properties directly on the myObject
var anotherObject = { a: 2 };
var myObject = Object.create(anotherObject);
myObject.b = 3;
console.log(myObject); // {b: 3}
shield
[[The Prototype]] If the upper chain appears in a name attribute myObject, there is also that of the shield will occur.
myObject contains a shielding property of a prototype of all upper chain properties.
var anotherObject = { a: 2 };
var myObject = Object.create(anotherObject);
Object.defineProperty(myObject, 'a', { value: 3, writable: true });
console.log(myObject); // {a: 3}
console.log(anotherObject); // {a: 2}
myObject.a = 4;
console.log(anotherObject); // {a: 2}
console.log(myObject); // {a: 4}
If not present in a myObject, present in the anotherObject, myObject.a = 3, there will be three special cases
- If [[the Prototype]] the presence of a top chain, and not read (writable: true), then added in an attribute named a myObject, it is the shielding properties.
var anotherObject = { a: 2 };
var myObject = Object.create(anotherObject);
console.log(myObject.a); // 2
myObject.a = 3;
console.log(myObject.a); // 3
console.log(myObject); // {a: 3}
console.log(anotherObject); // {a: 2}
- If [[the Prototype]] the presence of the upper strand a, but read-only (writable: false), it can not be added to the myObject a. If strict mode error. But can be defined in definePrototype a.
// 'use strict'; // Uncaught TypeError: Cannot assign to read only property 'a' of object '#<Object>'
var anotherObject = {};
Object.defineProperties(anotherObject, {
a: {
writable: false,
value: 2
}
});
var myObject = Object.create(anotherObject);
console.log(myObject.a); // 2
myObject.a = 3;
console.log(myObject.a); // 2
console.log(myObject); // {}
console.log(anotherObject); // {a: 2}
Object.defineProperty(myObject, 'a', { value: 3 });
console.log(myObject); // {a: 3}
myObject.a = 4;
console.log(myObject); // {a: 4}
- If in a [[Prototype]] is a top setter, it is not added to the myObect, it will not redefine the setter, but calling this setter
var anotherObject = {
get a() {
return this._a_;
},
set a(val) {
this._a_ = val;
}
};
var myObject = Object.create(anotherObject);
console.log(myObject); // {}
myObject.a = 3;
console.log(myObject); // {_a_: 3}
Implicit shield
var anotherObject = { a: 2 };
var myObject = Object.create(anotherObject);
console.log(anotherObject.a); // 2
console.log(myObject.a); // 2
// 相当于 myObject = myObject.a + 1
myObject.a++;
console.log(anotherObject.a); // 2
console.log(myObject.a); // 3