Want to know how to set a function with object as a map or a weakmap. And how to Identify whether to use map or weakmap.
function testCases() {
let model = new JsModel({name: 'Harry', age: 20});
// few methods
console.log(model.get('name')) // 'Harry'
console.log(model.get('age')) // 20
// few other methods
model.set('name', 'Bob');
console.log(model.get('name')) // 'Bob'
console.log(model.has('name')) // true
model.unset('name');
console.log(model.has('name')) // false
const city = new JsModel(); // valid
city.set('name', 'San Jose');
city.set('population', 1000);
console.log( city.get('name')) // San Jose
console.log( city.get('population')) // 1000
console.log(city.get('noProp')) // undefined
}
testCases();
I am new to Javascript so want to understand if the above code like how to get these console.logs work.
I've written a lot of javascript and never used WeakMap. Here is how I'd write that code.
function testCases() {
const model = {name: 'Harry', age: 20};
// few methods
console.log(model.name) // 'Harry'
console.log(model.age) // 20
// few other methods
model.name = 'Bob';
console.log(model.name) // 'Bob'
console.log(model.name !== undefined) // true
delete model.name;
console.log(model.name !== undefined) // false
const city = {}; // valid
city.name = 'San Jose';
city.population = 1000;
console.log(city.name) // San Jose
console.log(city.population) // 1000
console.log(city.noProp) // undefined
}
testCases();