为什么Javascript在显示的数组元素中添加函数名
我有一个用Javascript编写的天气应用程序,其中包括一个温度构造函数(以及其他函数)。在没有详细说明的情况下,我注意到一个非常奇怪的行为,它使我的应用程序崩溃。当我创建一个温度对象数组并向其中添加一个对象,然后在控制台中显示它,然后替换为一个新的温度对象(具有相同的数据),然后再次在控制台中显示它时,它的显示方式不同。这是我的代码:为什么Javascript在显示的数组元素中添加函数名,javascript,arrays,inheritance,Javascript,Arrays,Inheritance,我有一个用Javascript编写的天气应用程序,其中包括一个温度构造函数(以及其他函数)。在没有详细说明的情况下,我注意到一个非常奇怪的行为,它使我的应用程序崩溃。当我创建一个温度对象数组并向其中添加一个对象,然后在控制台中显示它,然后替换为一个新的温度对象(具有相同的数据),然后再次在控制台中显示它时,它的显示方式不同。这是我的代码: let weatherData = []; weatherData[0] = new Temperature(200); console.log(weathe
let weatherData = [];
weatherData[0] = new Temperature(200);
console.log(weatherData[0]);
weatherData[0] = new Temperature(200);
console.log(weatherData[0]);
这是控制台输出:
Temperature {
value: 200,
}
{
value: 200,
}
因此,当我第一次向数组添加对象时,我不能使用温度从另一个类继承的方法。这就是我的意思:
let weatherData = [];
weatherData[0] = new Temperature(200);
weatherData[0].inheritedMethod();
/\抛出错误weatherData[0]。inheritedMethod()不是函数
let weatherData = [];
weatherData[0] = new Temperature(200);
weatherData[0] = new Temperature(200);
weatherData[0].inheritedMethod();
/\很好
有人能给我解释一下为什么会发生这种情况,我该如何解决
温度等级:
//inherits from WeatherData
const WeatherData = require("./WeatherData");
module.exports = function Temperature(time, place, type, unit, value) {
WeatherData.call(this, time, place, type, unit, value);
Temperature.prototype = Object.create(WeatherData.prototype);
Temperature.prototype.convertToF = function () {
...
};
Temperature.prototype.convertToC = function () {
...
};
};
所有这些问题都是由在构造函数中执行以下代码引起的:
Temperature.prototype = Object.create(WeatherData.prototype);
Temperature.prototype.convertToF = function () {
...
};
Temperature.prototype.convertToC = function () {
...
};
这应该放在构造函数之外,以便它立即执行并且只执行一次
控制台输出中的差异
首先,控制台可以自由地以自己的方式显示对象。尽管如此,这两个对象还是有着显著的区别:第一个对象是在上述分配给Temperature.prototype
的时间创建的,因此这个对象是在执行new Temperature()时隐式创建的
仍然是由原始的温度制成的。原型
但是,在创建第二个实例时,Temperature.prototype
已经更改,这直接影响了此
的构造方式。它现在是用WeatherData.prototype
作为原型构建的
请参见此代码段中的差异:
//设置
功能气象数据(时间、地点、类型、单位、值){
如果(时间!==未定义)this.time=时间;
}
WeatherData.prototype.inheritedMethod=函数(){};
功能温度(时间、地点、类型、单位、值){
WeatherData.call(此、时间、地点、类型、单位、值);
//错误定位代码:
Temperature.prototype=Object.create(WeatherData.prototype);
Temperature.prototype.convertToF=函数(){};
Temperature.prototype.convertToC=函数(){};
};
//测试用例:
console.log(Object.getPrototypeOf(新温度(200)).constructor.name);//温度
console.log(Object.getPrototypeOf(新温度(200)).constructor.name);//WeatherData
所有这些故障都是由在构造函数中执行以下代码引起的:
Temperature.prototype = Object.create(WeatherData.prototype);
Temperature.prototype.convertToF = function () {
...
};
Temperature.prototype.convertToC = function () {
...
};
这应该放在构造函数之外,以便它立即执行并且只执行一次
控制台输出中的差异
首先,控制台可以自由地以自己的方式显示对象。尽管如此,这两个对象还是有着显著的区别:第一个对象是在上述分配给Temperature.prototype
的时间创建的,因此这个对象是在执行new Temperature()时隐式创建的
仍然是由原始的温度制成的。原型
但是,在创建第二个实例时,Temperature.prototype
已经更改,这直接影响了此
的构造方式。它现在是用WeatherData.prototype
作为原型构建的
请参见此代码段中的差异:
//设置
功能气象数据(时间、地点、类型、单位、值){
如果(时间!==未定义)this.time=时间;
}
WeatherData.prototype.inheritedMethod=函数(){};
功能温度(时间、地点、类型、单位、值){
WeatherData.call(此、时间、地点、类型、单位、值);
//错误定位代码:
Temperature.prototype=Object.create(WeatherData.prototype);
Temperature.prototype.convertToF=函数(){};
Temperature.prototype.convertToC=函数(){};
};
//测试用例:
console.log(Object.getPrototypeOf(新温度(200)).constructor.name);//温度
console.log(Object.getPrototypeOf(新温度(200)).constructor.name);//WeatherData
没有看到温度的代码,很难说。请说明温度
类的定义位置。另外,做这件事的不是JavaScript,而是控制台API。控制台API在您习惯它的功能之前可能会让您感到非常沮丧。Temperature.prototype
的内容可能会超出Temperature函数的范围。不过,使用class-Temperature extends-WeatherData
语法可能会为您节省大量时间。^^这是一个很好的例子。每次创建新实例时,都会创建一个新的原型对象。这是破坏性的。如果看不到温度
的代码,很难说。请说明温度
类的定义位置。另外,做这件事的不是JavaScript,而是控制台API。控制台API在您习惯它的功能之前可能会让您感到非常沮丧。Temperature.prototype
的内容可能会超出Temperature函数的范围。不过,使用class-Temperature extends-WeatherData
语法可能会为您节省大量时间。^^这是一个很好的例子。每次创建新实例时,都会创建一个新的原型对象。这是破坏性的。