Object-defineProperty

writable: true, enumerable: true, configurable: true

				
					//case 1 - writable: true,enumerable: true,configurable: true,
let employee = {
  id: "empId-1",
  name: "Anurag",
};

Object.defineProperty(employee, "Job", {
  value: function () {
    return this.id + " " + this.name;
  },
  writable: true,
  enumerable: true,
  configurable: true,
});

console.log(employee); //{id: 'empId-1', name: 'Anurag', Job: Æ’}
console.log(employee.Job()); //empId-1 Anurag
				
			

writable: true, enumerable: true, configurable: true (changes from function to property)

				
					//case 2 - writable: true,enumerable: true,configurable: true
//Because you have made writable as true
//you can make changes from function to property
let employee = {
  id: "empId-1",
  name: "Anurag",
};

Object.defineProperty(employee, "Job", {
  value: function () {
    return this.id + " " + this.name;
  },
  writable: true,
  enumerable: true,
  configurable: true,
});
employee.Job = "I am now property not a function now.Below func call will be error";
console.log(employee); //{id: 'empId-1', name: 'Anurag', Job: Æ’}
console.log(employee.Job()); //Uncaught TypeError: employee.Job is not a function

				
			

writable: false,enumerable: true,configurable: true

				
					//case 3 - writable: false,enumerable: true,configurable: true
//To stop making changes from function to property , make writable to false
let employee = {
  id: "empId-1",
  name: "Anurag",
};
Object.defineProperty(employee, "Job", {
  value: function () {
    return this.id + " " + this.name;
  },
  writable: false,
  enumerable: true,
  configurable: true,
});
//Uncaught TypeError: Cannot assign to read only property 'Job' of object '#<Object>'
employee.Job = "Cannot assign to read only property 'Job' of object '#<Object>";
console.log(employee); //{id: 'empId-1', name: 'Anurag', Job: Æ’}
console.log(employee.Job()); //Uncaught TypeError: employee.Job is not a function

				
			

writable: false,enumerable: true,configurable: true (Enumerable true gives Job as one of the keys)

				
					//case 4 - writable: false,enumerable: true,configurable: true
//Because enumerable is true --> Object.keys(employee) -> gives me Job also as one property
let employee = {
  id: "empId-1",
  name: "Anurag",
};
Object.defineProperty(employee, "Job", {
  value: function () {
    return this.id + " " + this.name;
  },
  writable: false,
  enumerable: true,
  configurable: true,
});
console.log(employee); //{id: 'empId-1', name: 'Anurag', Job: Æ’}
console.log(Object.keys(employee)); //['id', 'name', 'Job']

				
			

writable: false,enumerable: false,configurable: true

				
					//case 5 - writable: false,enumerable: false,configurable: true
//Because enumerable is false --> Object.keys(employee) -> gives me ['id', 'name']
let employee = {
  id: "empId-1",
  name: "Anurag",
};
Object.defineProperty(employee, "Job", {
  value: function () {
    return this.id + " " + this.name;
  },
  writable: false,
  enumerable: false,
  configurable: true,
});
console.log(employee); //{id: 'empId-1', name: 'Anurag', Job: Æ’}
console.log(Object.keys(employee)); //['id', 'name']
				
			

writable: false,enumerable: false,configurable: true(redefine as configurable is true)

				
					//case 6 - writable: false,enumerable: true,configurable: true
//Because configurable is true.. I am able to redefine it, made writable to true
//as writable is true -->I made Job to property again
//As Job is now property --> I cannot call employee.Job()
let employee = {
  id: "empId-1",
  name: "Anurag",
};
Object.defineProperty(employee, "Job", {
  value: function () {
    return this.id + " " + this.name;
  },
  writable: false,
  enumerable: false,
  configurable: true,
});

Object.defineProperty(employee, "Job", {
  writable: true,
});
employee.Job = "I am property again!! as you made writable to true";
console.log(employee); //{id: 'empId-1', name: 'Anurag', Job: 'I am property again!! as you made writable to true'}
console.log(employee.Job()); //error->Uncaught TypeError: employee.Job is not a function

				
			

writable: false,enumerable: false,configurable: false(cannot redefine as configurable is false)

				
					//case 7 - writable: false,enumerable: false,configurable: false
//Because configurable is true.. I am able to redefine it, made writable to true
//as writable is true -->I made Job to property again
//As Job is now property --> I cannot call employee.Job()
let employee = {
  id: "empId-1",
  name: "Anurag",
};
Object.defineProperty(employee, "Job", {
  value: function () {
    return this.id + " " + this.name;
  },
  writable: false,
  enumerable: false,
  configurable: false,
});

//you get error while redefining - the below line
//Cannot redefine property: Job
Object.defineProperty(employee, "Job", {
  writable: true,
});
employee.Job = "I am property again!! as you made writable to true";
console.log(employee);
console.log(employee.Job());
				
			

Leave a Comment