Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to add properties to an object in JavaScript

2 Answers

0 votes
function employee(name, job, salary) {
    this.name = name;
    this.job = job;
    this.salary = salary;
}

employee.prototype.company = "collectivesolver";
 
let e1 = new employee("tom bankester", "programmer", 11890);
 
console.log("name: " + e1.name);
console.log("job: " + e1.job);
console.log("salary: $" + e1.salary);
console.log("company:" + e1.company);


 
/*
run:
 
name: tom bankester
job: programmer
salary: $11890
company:collectivesolver
 
*/

 



answered Apr 10, 2017 by avibootz
edited May 20, 2024 by avibootz
0 votes
const obj = { Name: "Tim" };
console.log(obj);
 
obj.Age = 42;
console.log(obj);
 
obj.Country = "USA";
console.log(obj);

  
   
      
/*
run:
            
{
  Name: "Tim"
}
{
  Age: 42,
  Name: "Tim"
}
{
  Age: 42,
  Country: "USA",
  Name: "Tim"
}
          
*/

 



answered May 20, 2024 by avibootz
...