Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript OOP设置方法无法正常工作_Javascript_Oop_Methods - Fatal编程技术网

Javascript OOP设置方法无法正常工作

Javascript OOP设置方法无法正常工作,javascript,oop,methods,Javascript,Oop,Methods,所以我在学习OOP。下面的例子不适合我。也不知道为什么。我已经将代码简化为两个变量。我的问题真的是在car1.setOdometer(1000)这条线在我尝试时不起作用。基本上,这一行会自动更改里程计读数变量。然后,当我点击按钮3时,我得到一条未定义的“英里”线 js function Car(Make, Miles) { this.make = Make; this.odometerReading = Miles; this.showInfo = function()

所以我在学习OOP。下面的例子不适合我。也不知道为什么。我已经将代码简化为两个变量。我的问题真的是在
car1.setOdometer(1000)
这条线在我尝试时不起作用。基本上,这一行会自动更改里程计读数变量。然后,当我点击按钮3时,我得到一条未定义的“英里”线

js

function Car(Make, Miles) {
    this.make = Make;
    this.odometerReading = Miles;
    this.showInfo = function() {
        alert(this.make + this.odometerReading);
    };
    this.setOdometer = function(newMiles) {
        this.odometerReading = "newMiles";
    };
}

var car1 = new Car("X", 50);
var car2 = new Car("Y", 75);
car1.setOdometerReading(1000);  //this doesn't work for me.
//It winds up changing odometerReading on car1.showInfo() from the onset!
html

<input type="button" value="Car1" onclick="car1.showInfo()">
<input type="button" value="Car2" onclick="car2.showInfo()">
<input type="button" value="Change Car1" onclick="car1.setOdometer()">
删除

car1.setOdometerReading(1000);

是我遗漏了什么,还是教程错了

我看到了类Cars和Car类型的新对象。

这里是一个工作实现的参考代码。正确设置
car1
里程计读数
。我想这些错误都是因为打字错误

<head>
<script>
function Car(Make, Miles){
    this.make = Make;
    this.odometerReading = Miles;

    this.showInfo = function() {
        alert(this.make + " " + this.odometerReading);
    }

    this.setOdometer = function(newMiles) {
        this.odometerReading = newMiles;
    }
}
var car1 = new Car("X", 50);
var car2 = new Car("Y", 75);
car1.setOdometer(1000);  //now it has the right function name and actually uses the parameter.
</script>
</head>

<body>
<input type="button" value="Car1" onclick="car1.showInfo()">
<input type="button" value="Car2" onclick="car2.showInfo()">
<input type="button" value="Change Car1" onclick="car1.setOdometer('something Else')">
</body>

多功能车(品牌,英里){
make=make;
此.odometer读数=英里;
this.showInfo=函数(){
警报(this.make+“”+this.odometerReading);
}
this.set里程表=功能(新英里){
this.odometer读数=新英里;
}
}
var car1=新车(“X”,50);
var car2=新车(“Y”,75);
car1.设定里程表(1000)//现在它有了正确的函数名并实际使用了参数。

此代码是否缺少从
功能车(品牌,英里数){
开始的闭合括号?该功能的名称也错误,它应该是
Car
,而不是
Cars
。setOdometerReading()未在任何地方定义。您是否从某处复制粘贴此代码或手动编写此代码?存在键入错误的函数名和其他小错误。请仔细检查所有内容。使用您的。使用您的浏览器控制台…此代码应引发多个错误。在询问此问题之前,您至少应该看到这些错误…并将它们包含在qu中估计。如果不是这样的话,或者更小心地将代码复制到问题中。我认为您所做的错误只是调用了错误的函数(
setOdometer阅读
,但应该是
setOdometer
)。此外,函数没有使用参数
newMiles
,而是将其设置为静态字符串(
this.odometerReading=“newMiles”
),这也很奇怪。它应该是car。对不起,这个东西是50,永远不会被car1调用。它应该是make x和miles 50,并使用参数,这样就不需要在onclick事件中使用它。
odometerReading=50
通过调用
setOdometer(1000)立即被覆盖
。如果不使用该行,它的值将为
50
。我让
setOdometer()
使用该参数,因为它实际上是一个setter函数,将其设置为静态字符串对我来说没有意义。
<head>
<script>
function Car(Make, Miles){
    this.make = Make;
    this.odometerReading = Miles;

    this.showInfo = function() {
        alert(this.make + " " + this.odometerReading);
    }

    this.setOdometer = function(newMiles) {
        this.odometerReading = newMiles;
    }
}
var car1 = new Car("X", 50);
var car2 = new Car("Y", 75);
car1.setOdometer(1000);  //now it has the right function name and actually uses the parameter.
</script>
</head>

<body>
<input type="button" value="Car1" onclick="car1.showInfo()">
<input type="button" value="Car2" onclick="car2.showInfo()">
<input type="button" value="Change Car1" onclick="car1.setOdometer('something Else')">
</body>