In this example let us create a Javascript program that will sum up the salaries of two workers. Our plan is to create an object with the metaprogramming concept and then use that object, to sum up, the salaries of two workers. The solution is as follows:-
let salary = Object.defineProperties({}, { a : {value : 2000, writable: true, enumerable: true, configurable: true}, b : {value : 3000, writable: true, enumerable: true, configurable: true}, sum : {get() {return this.a + this.b;}, enumerable: true, configurable: true} }); console.log(salary.sum) // 5000 salary.a = 3000 salary.b = 10000 console.log(salary.sum) // 13000
As you can see, the above program will define various properties with various attributes, the last one will have the get method which will return the sum of two workers. In the first printout, the sum just returns the total sum before any adjustment of the salaries and in the second printout, the sum has been adjusted due to the revision of the salaries of both workers.
This metaprogramming method of writing Javascript is a lot shorter than the other traditional method…