有人可以向我解释为什么这不会运行?

时间:2015-06-16 00:34:18

标签: javascript html class object

我应该创建一个类,然后使用该类创建一个具有相同属性的新对象,显示它们,更改价格,然后显示新信息。我很困惑。我甚至不知道我哪里出错了。

<!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
       function Television(manufac, scrnSize, retPrice) {
       myTV.manufacturer = manufac
       myTV.diagScrnSize = scrnSize
       myTV.retailPrice = retPrice
       myTV.setPrice = setPrice()
       myTV.displayInfo = displayInfo()
    };
    </script>
    </head>

    <body>
    <script type="text/javascript">
    // Display function
        function displayInfo() {
        document.write("Manufacturer:  + myTV.manufacturer + BR");
        document.write("Screen Diagonal Size:  + myTV.diagonalSize + BR");
        document.write("Retail Price: + myTV.retailPrice + BR");
        document.write("Thank you!");
    };
    // Set price function
        function setPrice() {
        p = parsefloat(p);
        this.retailPrice = setPrice;
    };
       function displayInfo() {
        document.write("Manufacturer:  + myTV.manufacturer + BR");
        document.write("Screen Diagonal Size:  + myTV.diagonalSize + BR");
        document.write("Retail Price: + myTV.retailPrice + BR");
        document.write("Thank you!");
        };
        var BR = "<br />"
        var myTV = new Television("Sony", 52, 1299.00);

    // Run the program
        myTV.displayInfo()
        myTV.setPrice() 
        myTV.displayInfo()
    </script>
    </body>
    </html>

1 个答案:

答案 0 :(得分:1)

试试这个。那里你有很多错误。比较你的,看看你哪里出错了。

&#13;
&#13;
function Television(manufac, scrnSize, retPrice) {
  this.manufacturer = manufac;
  this.diagScrnSize = scrnSize;
  this.retailPrice = retPrice;
  this.setPrice = function(p){
    p = parseFloat(p);
    this.retailPrice = p;
  }
  this.displayInfo = function(){
        document.write("Manufacturer:"  + myTV.manufacturer + BR);
        document.write("Screen Diagonal Size:"  + myTV.diagScrnSize + BR);
        document.write("Retail Price:" + myTV.retailPrice + BR);
        document.write("Thank you!" + BR);
    
  };
};
var BR = "<br />";
var myTV = new Television("Sony", 52, 1299.00);
myTV.displayInfo();
myTV.setPrice(1099.00);
myTV.displayInfo();
&#13;
&#13;
&#13;

确保定义显示功能。我只看到评论,没有声明