我在调用我创建的构造函数内部的方法时遇到问题。这是函数,我尝试调用它...
var Cohort = function(program, campus, number, students){
this.program = program,
this.campus = campus,
this.number = number,
this.students = students,
function sayName(){
return `This cohort is called ${program}, ${campus}, ${number}.`
},
function takeAttendance(){
return console.log(students)
}
}
var cohort1 = new Cohort("w", "pr", 27, ['devin', 'ben', 'bob'])
var cohort2 = new Cohort('w', 'pr', 31, ["Brendan Eich", "Dan Abramov", "Wes Bos", "Kent Dodds"])
cohort1.sayName()
控制台显示 cohort1.sayName 不是函数。
最佳答案
您必须将方法设置到原型(prototype)上。您在代码中所做的只是简单地将函数声明为 Cohort
函数范围的局部函数,因此它们不是方法。
每当您调用 new Cohort(...)
时,都会从 链接一个对象(
,它成为新 new Cohort().__proto__ === Cohort.prototype
) >Cohort.prototypeCohort
对象的 this
,您的属性将保存到该对象中。设置 Cohort.prototype.methods
使用原型(prototype)链逻辑允许在 Cohort
对象的每个实例上调用这些方法
var Cohort = function(program, campus, number, students) {
this.program = program
this.campus = campus
this.number = number
this.students = students
}
Cohort.prototype.sayName = function() {
return `This cohort is called ${this.program}, ${this.campus}, ${this.number}.`
}
Cohort.prototype.takeAttendance = function() {
return console.log(this.students)
}
var cohort1 = new Cohort("w", "pr", 27, ['devin', 'ben', 'bob'])
var cohort2 = new Cohort('w', 'pr', 31, ["Brendan Eich", "Dan Abramov", "Wes Bos", "Kent Dodds"])
console.log(cohort1.sayName())
cohort1.takeAttendance()
console.log(Object.getOwnPropertyNames(cohort1))
console.log(Object.getOwnPropertyNames(cohort1.__proto__))
在 ES6 中,你可以简单地做
class Cohort {
constructor(program, campus, number, students) {
this.program = program
this.campus = campus
this.number = number
this.students = students
}
sayName() {
return `This cohort is called ${this.program}, ${this.campus}, ${this.number}.`
}
takeAttendance() {
return console.log(this.students)
}
}
var cohort1 = new Cohort("w", "pr", 27, ['devin', 'ben', 'bob'])
var cohort2 = new Cohort('w', 'pr', 31, ["Brendan Eich", "Dan Abramov", "Wes Bos", "Kent Dodds"])
console.log(cohort1.sayName())
cohort1.takeAttendance()
console.log(Object.getOwnPropertyNames(cohort1))
console.log(Object.getOwnPropertyNames(cohort1.__proto__))
这个 class
构造实际上只是一个语法糖,实现了与 ES5 中相同的逻辑
作为额外说明,以下代码也有效,但通常不是首选。注意方法存储位置的不同(检查最后 2 个 console.log
并与上面的进行比较)
var Cohort = function(program, campus, number, students) {
this.program = program
this.campus = campus
this.number = number
this.students = students
this.sayName = function() {
return `This cohort is called ${this.program}, ${this.campus}, ${this.number}.`
}
this.takeAttendance = function() {
return console.log(this.students)
}
}
var cohort1 = new Cohort("w", "pr", 27, ['devin', 'ben', 'bob'])
var cohort2 = new Cohort('w', 'pr', 31, ["Brendan Eich", "Dan Abramov", "Wes Bos", "Kent Dodds"])
console.log(cohort1.sayName())
cohort1.takeAttendance()
console.log(Object.getOwnPropertyNames(cohort1))
console.log(Object.getOwnPropertyNames(cohort1.__proto__))
关于javascript - 调用 javascript 构造函数方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49241195/