Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
How to set a function as map or weakmap with following code?

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!