javascript 调用方法有问题
Issue with javascript call method
我想从 car 构造函数访问 Vehicles 的详细信息方法,并希望通过调用而不继承 Car 中的 Vehicles 来打印名称和类型。这是我的代码:
function Vehicles(name, type) {
// console.log(name);
this.name = name;
this.type = type;
this.getDetails = function() {
console.log(this.name, this.type);
}
}
function car(name, type) {
this.NewVehicles = new Vehicles(name, type);
// this.details = this.call(this.NewVehicles.getDetails);
this.details = this.NewVehicles.call(this.getDetails);
}
const newCar = new car();
console.log(newCar.details("ss", "zxxzx"));
但我收到以下错误:
this.details = this.NewVehicles.call(this.getDetails); ^
TypeError: this.NewVehicles.call is not a function
at new car (/temp/file.js:16:35)
at Object.<anonymous> (/temp/file.js:19:16)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
调用newCar.details和call方法时,有些概念错误,有些错误。如果您能帮助解决问题,我们将不胜感激。
call() 的第一个参数应该是“调用 func 时用作此值的值”。参见:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
至于继承:
function Vehicle(name, type) {
this.name = name;
this.type = type;
this.getDetails = function() {
console.log(this.name, this.type);
}
}
function Car(name, type) {
Vehicle.call(this, name, type);
}
const myCar = new Car("foo", "bar");
myCar.getDetails();
应阅读:https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance
我想从 car 构造函数访问 Vehicles 的详细信息方法,并希望通过调用而不继承 Car 中的 Vehicles 来打印名称和类型。这是我的代码:
function Vehicles(name, type) {
// console.log(name);
this.name = name;
this.type = type;
this.getDetails = function() {
console.log(this.name, this.type);
}
}
function car(name, type) {
this.NewVehicles = new Vehicles(name, type);
// this.details = this.call(this.NewVehicles.getDetails);
this.details = this.NewVehicles.call(this.getDetails);
}
const newCar = new car();
console.log(newCar.details("ss", "zxxzx"));
但我收到以下错误:
this.details = this.NewVehicles.call(this.getDetails); ^
TypeError: this.NewVehicles.call is not a function
at new car (/temp/file.js:16:35)
at Object.<anonymous> (/temp/file.js:19:16)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
调用newCar.details和call方法时,有些概念错误,有些错误。如果您能帮助解决问题,我们将不胜感激。
call() 的第一个参数应该是“调用 func 时用作此值的值”。参见:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
至于继承:
function Vehicle(name, type) {
this.name = name;
this.type = type;
this.getDetails = function() {
console.log(this.name, this.type);
}
}
function Car(name, type) {
Vehicle.call(this, name, type);
}
const myCar = new Car("foo", "bar");
myCar.getDetails();
应阅读:https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance