TypeError: is not a function for the inheriting object function
TypeError: is not a function for the inheriting object function
我只是想用简单的代码来理解原型继承。
function Place() {
}
Place.prototype.airportCode = function(code) {
console.log('Airport code is: ' + code);
}
function City(cityName) {
this.name = cityName;
}
City.prototype.railwayStateCode = function(code) {
console.log('Railway station code is: ' + code);
}
City.prototype = Object.create(Place.prototype);
const sydney = new City('Sydney');
const melbourne = new Place();
当我尝试
sydney.railwayStateCode('SYD');
我收到一个错误
TypeError: sydney.railwayStateCode is not a function
据我了解,应该不会报错。我做错了什么吗?
您在这里重写了原型:
City.prototype = Object.create(Place.prototype);
要使其正常工作,请像这样更改顺序:
City.prototype = Object.create(Place.prototype);
City.prototype.railwayStateCode = function(code) {
console.log('Railway station code is: ' + code);
}
我只是想用简单的代码来理解原型继承。
function Place() {
}
Place.prototype.airportCode = function(code) {
console.log('Airport code is: ' + code);
}
function City(cityName) {
this.name = cityName;
}
City.prototype.railwayStateCode = function(code) {
console.log('Railway station code is: ' + code);
}
City.prototype = Object.create(Place.prototype);
const sydney = new City('Sydney');
const melbourne = new Place();
当我尝试
sydney.railwayStateCode('SYD');
我收到一个错误
TypeError: sydney.railwayStateCode is not a function
据我了解,应该不会报错。我做错了什么吗?
您在这里重写了原型:
City.prototype = Object.create(Place.prototype);
要使其正常工作,请像这样更改顺序:
City.prototype = Object.create(Place.prototype);
City.prototype.railwayStateCode = function(code) {
console.log('Railway station code is: ' + code);
}