Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

please see what is wrong with my object literal

Posted on 2012-03-18
5
Medium Priority
?
176 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: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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

824 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