调用javascript构造函数方法

时间:2018-03-12 17:28:39

标签: 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不是函数。

2 个答案:

答案 0 :(得分:3)

您必须将方法设置到原型上。您在代码中所做的只是声明Cohort函数范围的本地函数,因此它们不是方法。

每当您致电new Cohort(...)时,对象都会被new Cohort().__proto__ === Cohort.prototype链接起来Cohort.prototype,而this会成为新Cohort对象的Cohort.prototype.methods。属性将保存到。设置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__))对象的每个实例上调用这些方法



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__))




在ES6中,您只需执行

即可



class




这个console.log构造实际上只是一个与ES5实现相同逻辑的语法糖

作为额外注释,以下代码也有效,但通常不是首选。注意方法存储位置的不同(检查最后2 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__))并与上面的方法进行比较)



webView.scrollView.contentInsetAdjustmentBehavior = .never




答案 1 :(得分:1)

要在函数内部使用函数,必须使用与分配属性相同的方式进行分配。

您可以通过添加SELECT district_name, sum("Population") as total, sum(CASE WHEN "Cluster" IN ('city', 'town') THEN "Population" ELSE 0 END ) as district_total, sum(CASE WHEN "Cluster" IN ('city', 'town') THEN "Population" ELSE 0 END ) * 100.0 / sum("Population") as PP FROM residentioal GROUP BY district_name;

来实现
this

...或使用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(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()) (大多数开发人员的首选方法):

prototype