Solved

please see what is wrong with my object literal

Posted on 2012-03-18
5
168 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript code not working 3 33
Use "if not" in a condition 2 19
Html split(text) Part2 6 23
how to customize the text in the legend in highcharts 3 35
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

726 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