obj = {
_a: [],
get a(){
return this._a;
},
set a(value){
if (this._a.includes(value)){
console.warn(`${value} already in array`);
return;
}
this._a.push(value);
}
}
obj.a = 3;
obj.a = 4;
obj.a = 5;
obj.a = 3;
console.log(obj.a);
lets say for whatever reason we need an array which can only contain unique values, we can bypass the setter, but then we also bypass the validation (which is done in the setter) which can cause issues further down in the code
Just did some Googling to see if we could create hidden properties for objects. I wanted to see if I could hide the property in a way so that even if you look at the length of the object it would not be counted. Also if you happened to find the property it can not be changed.
Here I have defined my robot object. Notice that the _enegeryLevel property has been removed.
const robot = {
recharge(){
this._energyLevel += 30;
console.log(`Recharged! Energy is currently at ${this._energyLevel}%.`)
}
};
Here I am able to use the keys() method to check how many properties my object has.
let keyCount = Object.keys(robot);
console.log(keyCount.length);
// Output = 1
Then I use defineProperty method with my object. It creates the property _energyLevel and sets it some rules.
Object.freeze() will prevent properties from being altered, right? Though I don’t know if this lesson wants us to prevent elements from being altered. Instead, I think they want us to safely change the value of a property (if the value of a property couldn’t be 3, we could use setters to assign whatever value the developer wants, except if it was 3)
In this topic about privacy, it is mentioned that an underscore must be added before the property name as a naming convention so that others can understand that the property must not be mutated. However, if an underscore is added, shouldn’t the property name be declared as a string. In the earlier chapters, I learnt that any special characters used in keys must be declared as a string. Am I missing something?
Can anybody please illuminate me? Have I done something weird in this exercise, because they said that a ‘funky string’ is printed to the console, but I was expecting it to look like this ‘high30%’ and why is there an undefined in my code? Can anybody please tell me what is the ‘type-coercion’ thing is supposed to make me see? My code below:
const robot = {
_energyLevel: 100,
// this recharge() method adds 30 to the energy level
recharge(){
this._energyLevel += 30;
console.log(Recharged! Energy is currently at ${this._energyLevel}%.)
}
};
// this line reassigns the ‘_energyLevel’ property from the robot object to ‘high’
robot._energyLevel = ‘high’;
// this line prints the text contained within the recharge() method and modified using the reassignments above
console.log(robot.recharge());
Why doesn’t robot[_energyLevel] = ‘high’; work to reassign the value of the _energyLevel property?
I thought that you could use brackets to access any property. I also understood that dot notation cannot be used with properties that have special characters. Why isn’t an underscore _ considered a special character?