Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

please see what is wrong with my object literal

Posted on 2012-03-18
5
Medium Priority
?
172 Views
Last Modified: 2012-03-18
I get an "Car.display is not a function Car.display();" error.

Not sure why this is happening as I have included the method in the object.

Can someone please take a look?

thanks...

<html>
<head><title>Object object</title>
<script type="text/javascript">

var Car = {
            make:undefined,
            year:2006,
            price:undefined,
            owner : {
                        name:"Henry Lee",
                        cell_phone:"222-222-2222",
                        address: {street:"10 Main",
                                      city:"SF",
                                      state:"CA"
},
dealer:"SF Honda",
display: function() {details="Make: "+Car.make+"\n";
                              details += "Year:"+Car.year+"\n";
                              alert(details);
                              }
                        }
};
     
</script>
</head>
<body bgColor="#EOFFFF">
<script type="text/javascript">

Car.make="Honda Civic";
Car.year=2009;
Car.display();

</script>
</body>
</html>
0
Comment
Question by:jecommera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:Proculopsis
ID: 37734989
The display method is not where you're expecting it - try:

Car.owner.display()
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 37734990
0
 

Author Comment

by:jecommera
ID: 37734994
@ Proculopsis - that worked however I don't understand since the display method is a property of Car not owner?

 owner : {
                        name:"Henry Lee",
                        cell_phone:"222-222-2222",
                        address: {street:"10 Main",
                                      city:"SF",
                                      state:"CA"
                              }
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 1600 total points
ID: 37735398
If you want it to be a method of Car then use the following instead:

var Car = {
    make: undefined,
    year: 2006,
    price: undefined,
    owner: {
        name: "Henry Lee",
        cell_phone: "222-222-2222",
        address: {
            street: "10 Main",
            city: "SF",
            state: "CA"
        },
        dealer: "SF Honda"
    },
    display: function() {
        details = "Make: " + Car.make + "\n";
        details += "Year:" + Car.year + "\n";
        alert(details);

    }
};

Open in new window

0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 400 total points
ID: 37735409
with respect to comment #37734994

I think you by mistake added the method to the owner property rather than the Car itself.
You simply need to add another closing brace before that method to make it work as
Car.display()
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question