javascript - 调用 javascript 构造函数方法

标签 javascript function methods constructor

我在调用我创建的构造函数内部的方法时遇到问题。这是函数,我尝试调用它...

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.prototype,它成为新 Cohort 对象的 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/

相关文章:

javascript - Vuetify 多个 v-select 所需规则不起作用

java - Java 中方法修饰符的顺序

string - shell bash,你能检查一个函数是否存在吗

javascript - 如何从javascript中带有空参数的函数返回零?

Java 在方法调用中声明一个 String 数组

c# - 如何将参数作为单个对象传递?

javascript - IE 错误与 jQuery 和超时

javascript - 通过 https 的 SSE 不工作

javascript - TypeError : (intermediate value). 那么不是一个函数

c - 使用数组打印出一些星号,这取决于用户在 C 中输入的数字