简体   繁体   English

我的javascript构造函数和相关语法有什么问题

[英]what is wrong with my javascript constructors and related syntax

EDIT: Thank you to all who responded. 编辑:谢谢所有答复。 I'm in the process of checking it with just console.log for the desired output, but at the moment I do know that it is not doing what I want with HTML. 我正在仅使用console.log检查它的期望输出,但是此刻我确实知道它没有按照我的意愿执行HTML。 Anyone else know what could be wrong? 其他人知道怎么了吗?

Thanks! 谢谢!

EDIT 2: The function "functions" when I define "television" directly within the console, but other than that, I can't figure out how to have television defined in the code. 编辑2:当我直接在控制台中定义“电视”时,功能“功能”,但除此之外,我不知道如何在代码中定义电视。 I took it out of the start(); 我从start()中取出它; function because I thought it might be out of scope. 功能,因为我认为它可能超出范围。 That didn't work either. 那也不起作用。

// Program Name: Television Store
// Television Store Website.

    /* Constructor Function: */
        function TelInput(ret, man, scr, conf, dis) {
        this.ret = parseInt(ret, 10);
        this.man = man;
        this.scr = parseInt(scr, 10);
        this.discount = this.ret * 0.9;
        this.dis = dis;
        this.conf = conf  
    };

var start = function(){    
/* Initial Variables */
var screen = prompt("Enter the screen size of TV in inches: ");
var manufacturer = prompt("Enter the manufacturer of TV: ");
var retail = prompt("Enter the retail value of TV: ");
var priceConf = confirm("Apply the 10% discount?");
var dispValues = confirm("Display Values when Finsihed?");

/* Display the Values */

console.log(television.ret);
console.log(television.man);
console.log(television.scr);
console.log(television.discount);

    if(television.conf){
        document.getElementById("l4").innerHtml = "Discounted Price:  " + television.dis;   
    }
};
console.log("Program Completed."); // Confirm Javascript functions.
start(); // Start function.
var television = new TelInput(retail, manufacturer, screen, priceConf, dispValues); // Create object

If telInput is supposed to be constructor function: 如果telInput应该是构造函数:

  • Call it with new : var television = new telInput(retail, manufacturer, screen, priceConf, dispValues); new调用它: var television = new telInput(retail, manufacturer, screen, priceConf, dispValues); . Right now the properties ret , man , etc will be assigned to window and television will be undefined . 现在,属性retman等将分配给window并且televisionundefined

  • Pascal-case constructor functions: TelInput . Pascal案例构造函数: TelInput This is a very strong convention. 这是非常严格的约定。 See this . 看到这个

A few other issues: 其他一些问题:

  • You are not assigning dis and conf to any property: add this.dis = dis; this.conf = conf 您没有将disconf分配给任何属性:add this.dis = dis; this.conf = conf this.dis = dis; this.conf = conf . this.dis = dis; this.conf = conf

  • this.discount = ret * 0.9; this should probably be this.discount = this.ret * 0.9; 这可能应该是this.discount = this.ret * 0.9; as this.ret contains the parsed value. 因为this.ret包含解析的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM