Javascript类型错误:无法读取属性';物种';未定义的

Javascript类型错误:无法读取属性';物种';未定义的,javascript,Javascript,我正在Codecademy上学习Javascript,我正在做一项任务,我必须记录两个人的物种(智人)和年龄。它返回了一个TypeError,一个我还没有看到的错误类型。错误是: TypeError: Cannot read property 'species' of undefined 我的代码是: function Person(name,age) { this.name = name; this.age = age; this.species = "Homo Sapiens"

我正在Codecademy上学习Javascript,我正在做一项任务,我必须记录两个人的物种(智人)和年龄。它返回了一个TypeError,一个我还没有看到的错误类型。错误是:

TypeError: Cannot read property 'species' of undefined
我的代码是:

function Person(name,age) {
  this.name = name;
  this.age = age;
  this.species = "Homo Sapiens";
}

var sally = Person("Sally Bowles",39);
var holden = Person("Holden Caulfield",16);
console.log("sally's species is " + sally.species + " and she is " + sally.age);
console.log("holden's species is " + holden.species + " and he is " + holden.age);
非常感谢您提供的任何帮助或建议

您必须使用运算符来创建对象类型的实例:

var sally = new Person('Sally Bowles', 39);
var holden = new Person('Holden Caulfield', 16);

console.log( sally.species, holden.species );  // "Homo Sapiens"
必须使用运算符来创建对象类型的实例:

var sally = new Person('Sally Bowles', 39);
var holden = new Person('Holden Caulfield', 16);

console.log( sally.species, holden.species );  // "Homo Sapiens"
必须使用运算符来创建对象类型的实例:

var sally = new Person('Sally Bowles', 39);
var holden = new Person('Holden Caulfield', 16);

console.log( sally.species, holden.species );  // "Homo Sapiens"
必须使用运算符来创建对象类型的实例:

var sally = new Person('Sally Bowles', 39);
var holden = new Person('Holden Caulfield', 16);

console.log( sally.species, holden.species );  // "Homo Sapiens"

VisioN告诉您缺少
new
关键字。 如果不指定
new
,会发生什么情况:

尝试将
记录在
人员

function Person(name,age) {
  this.name = name;
  this.age = age;
  this.species = "Homo Sapiens";
  console.log(this);
}
var sally=个人(“sally Bowles”,39)

var holden=新人(“holden Caulfield”,16)

//日志:

窗口{顶部:窗口,窗口:窗口,位置:位置, 外部:对象,chrome:Object…}Person{name:“Holden Caulfield”, 年龄:16岁,物种:“智人”}

在第一次调用
Sally Bowles
new
时,未指定
Person
,因此将
作为普通JavaScript函数调用,因此
设置为
窗口


然而,在对
Holden Caulfield
new的第二次调用中,指定了
Person
作为
constructor
函数,因此
将该
设置为您的新对象。

如VisioN所述,您缺少
new
关键字。 如果不指定
new
,会发生什么情况:

尝试将
记录在
人员

function Person(name,age) {
  this.name = name;
  this.age = age;
  this.species = "Homo Sapiens";
  console.log(this);
}
var sally=个人(“sally Bowles”,39)

var holden=新人(“holden Caulfield”,16)

//日志:

窗口{顶部:窗口,窗口:窗口,位置:位置, 外部:对象,chrome:Object…}Person{name:“Holden Caulfield”, 年龄:16岁,物种:“智人”}

在第一次调用
Sally Bowles
new
时,未指定
Person
,因此将
作为普通JavaScript函数调用,因此
设置为
窗口


然而,在对
Holden Caulfield
new的第二次调用中,指定了
Person
作为
constructor
函数,因此
将该
设置为您的新对象。

如VisioN所述,您缺少
new
关键字。 如果不指定
new
,会发生什么情况:

尝试将
记录在
人员

function Person(name,age) {
  this.name = name;
  this.age = age;
  this.species = "Homo Sapiens";
  console.log(this);
}
var sally=个人(“sally Bowles”,39)

var holden=新人(“holden Caulfield”,16)

//日志:

窗口{顶部:窗口,窗口:窗口,位置:位置, 外部:对象,chrome:Object…}Person{name:“Holden Caulfield”, 年龄:16岁,物种:“智人”}

在第一次调用
Sally Bowles
new
时,未指定
Person
,因此将
作为普通JavaScript函数调用,因此
设置为
窗口


然而,在对
Holden Caulfield
new的第二次调用中,指定了
Person
作为
constructor
函数,因此
将该
设置为您的新对象。

如VisioN所述,您缺少
new
关键字。 如果不指定
new
,会发生什么情况:

尝试将
记录在
人员

function Person(name,age) {
  this.name = name;
  this.age = age;
  this.species = "Homo Sapiens";
  console.log(this);
}
var sally=个人(“sally Bowles”,39)

var holden=新人(“holden Caulfield”,16)

//日志:

窗口{顶部:窗口,窗口:窗口,位置:位置, 外部:对象,chrome:Object…}Person{name:“Holden Caulfield”, 年龄:16岁,物种:“智人”}

在第一次调用
Sally Bowles
new
时,未指定
Person
,因此将
作为普通JavaScript函数调用,因此
设置为
窗口

然而,在对
Holden Caulfield
new的第二次调用中,指定了
Person
作为
constructor
函数调用,因此
设置为新对象