This is the 25th day of my participation in the August Challenge
preface
There is a requirement to store a student’s subject scores in an object that only changes subject scores each time, and cannot add or delete subjects.
The requirement is to create an object with fixed attributes that cannot be added or deleted, but whose values can be changed.
Some of you might start with:
- First, define an object that meets the requirements:
// Declare the result store object
let reportObj = {};
// Add a subject to the score store object and set the subject property cannot be added or deleted, but the subject score can be modified
Object.defineProperties(reportObj, {
ChineseMark: {
enumerable: true.writable: true.configurable: false.value: 60
},
EnglishMark: {
enumerable: true.writable: true.configurable: false.value: 60}});Copy the code
- Then write the result:
// Save the subject scores
reportObj.ChineseMark = 99;
reportObj.EnglishMark = 95;
console.log(reportObj); // {ChineseMark: 99, EnglishMark: 95}
Copy the code
Delete attributes to try:
delete reportObj.ChineseMark; // false
console.log(reportObj); // {ChineseMark: 99, EnglishMark: 95}
Copy the code
This seems to be the case, so try adding attributes:
reportObj.PhysicsMark = 100;
console.log(reportObj); // {ChineseMark: 99, EnglishMark: 95, PhysicsMark: 100}
Copy the code
What’s going on? Why is it suddenly not quite up to scratch? Object.defineproperties () can only precisely control the attributes it adds, but if you add attributes to an Object, it has no control.
Here we use a simple interface method to achieve this phenomenal demand
Object.seal()
describe
Seal seal seal seal seal seal seal
As the name suggests, the object.seal () method is used to “seal” an Object by preventing it from adding new attributes and marking all of its existing attributes as unconfigurable. The value of the current property can be changed as long as it was previously writable.
role
In general, an object is extensible (new attributes can be added).
Sealing an object makes it impossible to add new properties, and all existing properties become unconfigurable. The effect of a property not being configurable is that the property becomes non-deletable, and a data property cannot be redefined as an accessor property, or vice versa. However, the value of the attribute can still be modified.
Attempts to delete a property of a sealed object or to convert a property of a sealed object from a data property to an accessor property result in a silent failure or TypeError (most common, but not unique, in strict mode).
const object1 = {
property1: 42
};
Object.seal(object1);
object1.property1 = 33;
console.log(object1.property1);
// expected output: 33
delete object1.property1; // cannot delete when sealed
console.log(object1.property1);
// expected output: 33
Copy the code
In summary, object.seal () does the following:
- Set object.preventExtension () to forbid new properties (absolutely exist)
- Disables the configuration of different signals (absolutely exists).
- Disallow changes to accessor properties (getters and setters)
grammar
Object.seal(obj)
Copy the code
parameter
The obj argument represents the object to be sealed.
The return value
The object being sealed.
Implementation requirements
Since we have such a useful method, we should make good use of it. Finally, we can perfect the requirements at the beginning of the article:
// Declare the score store object and its properties
let reportObj = {
ChineseMark: 60.EnglishMark: 60
};
// Seal the result object
let sealedReportObj = Object.seal(reportObj);
// Change the subject score
sealedReportObj.ChineseMark = 99;
sealedReportObj.EnglishMark = 97;
console.log(sealedReportObj); // {"ChineseMark": 99, "EnglishMark": 97}
Copy the code
Verify:
// Add attributes
sealedReportObj.PhysicsMark = 100;
console.log(sealedReportObj); // {"ChineseMark": 99, "EnglishMark": 97}
// Delete attributes
delete sealedReportObj.ChineseMark; // false
console.log(sealedReportObj); // {"ChineseMark": 99, "EnglishMark": 97}
Copy the code
You can see that the properties of the object are really not added or deleted, which is a simple implementation of the requirements.
extension
To determine whether an Object is “sealed”, we can use the object.issealed () method:
Object.isSealed(sealedReportObj); // true
Copy the code
Object.freeze()
The object.freeze () method is used to freeze an Object. The effect is literal: freeze an object so that its properties and property values are immutable. It is obviously not appropriate to implement this requirement.
In common
Object.seal() and object.freeze () have the following in common:
- The object becomes unextensible, meaning that no new attributes can be added.
- Each element in the object becomes unconfigurable, meaning that attributes cannot be deleted.
- Both methods can throw errors if used in ‘Use Strict’ mode.
The difference between
Object.seal() lets you modify the value of the property, but object.freeze () does not.
conclusion
The object-.seal () method has some similarities and differences with object-.freeze ().
~
Thanks for reading!
~
Learn interesting knowledge, meet interesting friends, shape interesting soul!
Hello everyone, I am the author of “programming Samadhi”, I am king Yi, my public account is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!
You come, with expectations, I have ink to welcome! You return, no matter gain or loss, only to yu Yun give each other!
Knowledge and skills should be paid equal attention to, internal force and external power should be repaired simultaneously, theory and practice should grasp both hands, both hands should be hard!
Reference Documents:
Object.seal()
Object.freeze()