-1

I cannot stop running into problems with this code. I figure I'm doing something monumentally wrong, and I figure asking people who know what they're doing would be the best thing to do.

The actual "problem" or assignment is to create a simple TV object. It has three characteristics. Make one. Call it. Display all the characteristics. Then it's done.

I declare variables and make a constructor. As per the rules of the assignment, both of these things must remain in the header of the document.

Next is the body. I want to create a new television object, called myTV, with those parameters. I create a function to recall the data back. Then I actively call the data back.

Somewhere along the lines I have logic problems, possible syntax problems, and who knows what else. My mind is reeling from staring at the same code for so long, so I'm going to grab something to drink and check back here in like 15. ugh.

<html>
<head>
<script type="text/javascript">

    var manuTV;
    var sizeTV;
    var priceTV;

    function Television(manu, size, price){
        this.manuTV = manu;
        this.sizeTV = size;
        this.priceTV = price;
    }

</script>
</head>
<body>
<script type="text/javascript">

    var myTV = new Television("Sony", 52, 999);

    function DisplayInfo(){
        document.write("Television Manufacturer: " + this.manuTV + "<br />");
        document.write("Size(in inches)" + this.sizeTV + "<br />");
        document.write("Price:" + this.priceTV + "<br />");
    }

    myTV.displayInfo();

</script>
</body>
</html>
Fred Wuerges
  • 1,965
  • 2
  • 21
  • 42
Xynene
  • 103
  • 2
  • 2
    `DisplayInfo` is not associated with `Television`. Perhaps you want to put that on the `Television.prototype`? – Daniel A. White Mar 12 '15 at 18:35
  • Copy of http://stackoverflow.com/questions/2653299/creating-new-objects-in-javascript . – Sameer Sawla Mar 12 '15 at 18:37
  • @SameerSawla Hm? I don't understand. I visited that link but it talks about the differences in layout prefrence inside constructors. That's not really my question here. We have to use this format, and no there format is a viable option in the scope of my class. – Xynene Mar 12 '15 at 18:42

1 Answers1

1

Your DisplayInfo method isn't related in any way to your Television. There are two easy things you can do to make it work. You can add DisplayInfo to Television's prototype.

Television.prototype.displayInfo = function(){
    document.write("Television Manufacturer: " + this.manuTV + "<br />");
    document.write("Size(in inches)" + this.sizeTV + "<br />");
    document.write("Price:" + this.priceTV + "<br />");
}

This adds the method to Television's prototype so any Television can use it (tv.displayInfo()). You could also do it this way:

function displayInfo(tv){
    document.write("Television Manufacturer: " + tv.manuTV + "<br />");
    document.write("Size(in inches)" + tv.sizeTV + "<br />");
    document.write("Price:" + tv.priceTV + "<br />");
}

Pass a Television object to DisplayInfo, (see the argument added to the method signature), then use tv instead of this. Then call the function like this:

displayInfo(myTv);
Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109