Solved

please see what is wrong with my object literal

Posted on 2012-03-18
5
170 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:gurvinder372
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 400 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:gurvinder372
gurvinder372 earned 100 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

623 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