Search⌘ K
AI Features

Solution Review: Catch the Error

Understand how to protect JavaScript object properties from unauthorized changes by combining Object.freeze and Object.seal methods. Learn how strict mode enables error detection and how to handle these errors using try/catch blocks to provide clear messages.

We'll cover the following...

Solution review

Node.js
"use strict";
var person = {
name: "Nishant"
};
Object.freeze(person);
Object.seal(person);
function func(){
try{
person.name = "xyz"
console.log(person.name);
}catch(e){
console.log("Cannot change name")
}
try{
person.age = 30;
console.log(person.age);
}
catch(e){
console.log("Cannot add property age")
}
try{
delete person.name
console.log(person.name);
}
catch(e){
console.log("Cannot delete person")
}
}
func()

Explanation

Your task was to modify the original code (given below) so that none of the operations, modification, addition, or deletion, could be performed on the given object ...

Node.js
var person = {
name: "Nishant"
};
function func(){
person.name = "xyz";
console.log(person.name)
person.age = 30;
console.log(person.age)
delete person.name;
console.log(person.name)
}
func()

In the ...