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.

39,895 questions

51,826 answers

573 users

How to define and use objects in JavaScript

5 Answers

0 votes
var worker = 
{
    firstName:"Teddy",
    lastName:"Bear",
    age:100,
}; 

document.write(worker.firstName + "<br />");
document.write(worker.lastName + "<br />");
document.write(worker.age + "<br />");

/*
run:

Teddy
Bear
100

*/

 



answered Jun 30, 2015 by avibootz
0 votes
var worker = new Object();

worker.firstName = "Teddy";
worker.lastName = "Bear";
worker.age = 100;

document.write(worker.firstName + "<br />");
document.write(worker.lastName + "<br />");
document.write(worker.age + "<br />");

/*
run:

Teddy
Bear
100

*/

 



answered Jun 30, 2015 by avibootz
0 votes
function worker(first_name, last_name, age) 
{
    this.firstName = first_name;
    this.lastName = last_name;
    this.age = age;
}

var w1 = new worker("Teddy", "Bear", 100);

document.write(w1.firstName + "<br />");
document.write(w1.lastName + "<br />");
document.write(w1.age + "<br />");

/*
run:

Teddy
Bear
100

*/

 



answered Jun 30, 2015 by avibootz
0 votes
var worker = 
{
    firstName:"Teddy",
    lastName:"Bear",
    age:100,
    
    print_details : function() 
    {
       return this.firstName + " " + this.lastName + " " + this.age;
    }
}; 

document.write(worker.print_details());

/*
run:

Teddy Bear 100 

*/

 



answered Jun 30, 2015 by avibootz
0 votes
var worker = 
{
    firstName:"Teddy",
    lastName:"Bear",
    age:100,
    
    print_details : function() 
    {
       return this.firstName + " " + this.lastName + " " + this.age;
    },
    
    changeAge : function(age) 
    {
        this.age = age;
    }
}; 

document.write(worker.print_details() + "<br />");
worker.changeAge(120);
document.write(worker.print_details() + "<br />");

/*
run:

Teddy Bear 100
Teddy Bear 120

*/

 



answered Jun 30, 2015 by avibootz

Related questions

1 answer 171 views
1 answer 187 views
2 answers 186 views
1 answer 171 views
1 answer 140 views
...