使用函数构造函数原型添加新函数
问题描述:
我想检查一下我的想法是否正确?使用函数构造函数原型添加新函数
代码:
function Person(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
Person.prototype.getFullName = function() {
return this.firstname + ' ' + this.lastname;
}
var john = new Person('Melo','Tang');
我叫下面的图片 “功能构造” 的代码。
function Person(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
当程序运行到这一行
var john = new Person('Melo','Tang');
答
我真的不明白你的图表或箭头或他们的颜色应该代表什么。
当程序运行到这一行
var john = new Person('Melo','Tang');
这里会发生什么事正是一个新的对象通过Person
构造结构,与Person.prototype
为原型。除了被用作新对象的原型之外,Person.prototype
没有被检查或咨询,目前对它的方法也没有做任何事情。仅在查找属性和方法(如调用john.getFullName()
时)才会查阅原型。
为了清楚起见,我会避免使用术语“功能构造函数对象”。把它称为“构造函数(function)”。
+0
torazaburo谢谢。据我所知,Prototype是一个对象的属性,Proto是一个对象。我称之为“函数构造函数对象”,因为我想强调函数构造函数是JS中的一个对象。我想问一个问题,“Person.prototype”是函数构造函数的一个属性?如果是,getFullName函数被添加到Person.prototype或proto Obj? –
“原型链”标签应位于红色箭头上。 –
我忘了谢谢,但除此之外有任何错误? –