Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用javascript构造函数函数方法_Javascript_Function_Methods_Constructor - Fatal编程技术网

调用javascript构造函数函数方法

调用javascript构造函数函数方法,javascript,function,methods,constructor,Javascript,Function,Methods,Constructor,在我创建的构造函数中调用方法时遇到问题。下面是函数和我调用它的尝试 var队列=功能项目、校园、数量、学生{ this.program=程序, 这个校园, this.number=number, 这个。学生=学生, 函数名{ return`这个队列称为${program}、${campus}、${number}` }, 参加活动{ 返回控制台 } } var cohort1=新的Cohortw,pr,27,['devin','ben','bob'] var cohort2=新的队列“w”,“p

在我创建的构造函数中调用方法时遇到问题。下面是函数和我调用它的尝试

var队列=功能项目、校园、数量、学生{ this.program=程序, 这个校园, this.number=number, 这个。学生=学生, 函数名{ return`这个队列称为${program}、${campus}、${number}` }, 参加活动{ 返回控制台 } } var cohort1=新的Cohortw,pr,27,['devin','ben','bob'] var cohort2=新的队列“w”,“pr”,31,[Brendan Eich,Dan Abramov,Wes Bos,Kent Dodds]
cohort1.sayName您必须将方法设置到原型上。您在代码中所做的只是声明队列函数作用域的本地函数,因此它们不是方法

无论何时调用新队列…,都会将对象链接到新队列中。uuu proto_uuu===队列.prototype来自队列.prototype,它将成为新队列对象的一部分,您的属性将保存到该对象中。设置队列.prototype.methods使用原型链逻辑允许对队列对象的每个实例调用这些方法

var队列=功能项目、校园、数量、学生{ this.program=程序 这个校园 这个数字=数字 这个是学生 } question.prototype.sayName=函数{ return`此队列称为${This.program},${This.campus},${This.number}` } 队列.原型.出勤=功能{ 返回console.logthis.students } var cohort1=新的Cohortw,pr,27,['devin','ben','bob'] var cohort2=新的队列“w”,“pr”,31,[Brendan Eich,Dan Abramov,Wes Bos,Kent Dodds] console.logcohort1.sayName 同伙1.出勤率 console.logObject.getOwnPropertyNamescohort1
console.logObject.getOwnPropertyNamescohort1.\uuuu proto\uuuu您必须将方法设置到原型上。您在代码中所做的只是声明队列函数作用域的本地函数,因此它们不是方法

无论何时调用新队列…,都会将对象链接到新队列中。uuu proto_uuu===队列.prototype来自队列.prototype,它将成为新队列对象的一部分,您的属性将保存到该对象中。设置队列.prototype.methods使用原型链逻辑允许对队列对象的每个实例调用这些方法

var队列=功能项目、校园、数量、学生{ this.program=程序 这个校园 这个数字=数字 这个是学生 } question.prototype.sayName=函数{ return`此队列称为${This.program},${This.campus},${This.number}` } 队列.原型.出勤=功能{ 返回console.logthis.students } var cohort1=新的Cohortw,pr,27,['devin','ben','bob'] var cohort2=新的队列“w”,“pr”,31,[Brendan Eich,Dan Abramov,Wes Bos,Kent Dodds] console.logcohort1.sayName 同伙1.出勤率 console.logObject.getOwnPropertyNamescohort1
console.logObject.getOwnPropertyNamescohort1.\uuuu proto\uuuu要在函数中使用函数,必须使用与分配属性相同的方式分配它

您可以通过添加以下内容来完成此操作:

…或对大多数开发人员使用原型首选方法:

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

要在函数中使用函数,必须使用与指定属性相同的方法来指定它

您可以通过添加以下内容来完成此操作:

…或对大多数开发人员使用原型首选方法:

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

如果希望以指定的方式使用该方法,则需要将该方法添加到队列的原型中。有关详细信息,请参阅。在构造函数内声明函数不会使其成为构造对象的属性值。这只是一个局部函数。您可以通过添加此属性将其设置为属性。sayName=sayName@很有意义,但这不只是打印出所提到的变量的局部值吗?如果有什么东西改变了这个程序,sayName的输出不会反映它,对吗?换句话说,我认为你发布的内容会起作用,但我不确定它是否反映了OP的意图,尽管谁知道呢。@rmlan no;sayName函数引用构造函数参数,而不是对象属性。这些永远不会改变。@Pointy,我就是这么说的。我不能确定,但我有一种感觉与OP的实际需求相矛盾。如果你想以指定的方式使用它,你需要将该方法添加到队列的原型中。有关详细信息,请参阅。在构造函数内声明函数不会使其成为构造对象的属性值。这只是一个局部函数。您可以通过添加此属性将其设置为属性。sayName=sayName@很有意义,但这不只是打印出所提到的变量的局部值吗?如果有什么东西改变了这个程序,sayName的输出不会反映它,对吗?换言之,我认为你发布的内容会起作用,但我不确定它是否反映了OP的意图
没有;sayName函数引用构造函数参数,而不是对象属性。这些永远不会改变。@Pointy,我就是这么说的。我不能确定,但我有一种感觉,与OP实际想要的东西相矛盾。非常感谢,我以前试过作为一个班级做,但不起作用,这更有意义。非常感谢,我以前尝试作为一个班级做,但不起作用,这更有意义。