Solved

please see what is wrong with my object literal

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now