Javascript 原型问题,如何在原型中调用没有 this 的函数?
Javascript Prototype question, how do I call a function without this inside a prototype?
let Person = function (name, age) {
this.name = name;
this.age = age;
};
Person.prototype.testProto = function () {
console.log(this.name + " == " + this.age);
let xx = function() {
console.log("in xx");
}
};
let person = new Person("Jake",49);
person.testProto();
如果我将“let xx”更改为“this.xx”,这将起作用
并用 person.xx();
调用它
但不使用“this”,当 person.testProto.xx() 不起作用时如何调用它?
谢谢
return 来自函数内部的 xx 变量。
这将在不使用 this
的情况下调用 testProto 函数和 xx 函数。
let Person = function(name, age) {
this.name = name;
this.age = age;
};
Person.prototype.testProto = function() {
console.log(this.name + " == " + this.age);
let xx = function() {
console.log("in xx");
}
return xx;
};
let person = new Person("Jake", 49);
person.testProto()();
let Person = function (name, age) {
this.name = name;
this.age = age;
};
Person.prototype.testProto = function () {
console.log(this.name + " == " + this.age);
let xx = function() {
console.log("in xx");
}
};
let person = new Person("Jake",49);
person.testProto();
如果我将“let xx”更改为“this.xx”,这将起作用 并用 person.xx();
调用它但不使用“this”,当 person.testProto.xx() 不起作用时如何调用它?
谢谢
return 来自函数内部的 xx 变量。
这将在不使用 this
的情况下调用 testProto 函数和 xx 函数。
let Person = function(name, age) {
this.name = name;
this.age = age;
};
Person.prototype.testProto = function() {
console.log(this.name + " == " + this.age);
let xx = function() {
console.log("in xx");
}
return xx;
};
let person = new Person("Jake", 49);
person.testProto()();