从 JavaScript 继承方法访问变量
我正在研究从父对象继承到子对象的方法对变量的可访问性。
继承是通过将父类的实例与子类构造函数的原型相关联来执行的:
Function.prototype.extend = function(child) {
var prototype = new this;
child.prototype = prototype;
return child;
};
因此我定义了一个父类:
var Parent = function() {
var self = this;
this.init = function() {
self.name = 'Abram';
};
this.getName = function() {
alert('self.name = ' + self.name);
};
this.init();
};
和一个子类:
var Baby = Parent.extend(function(name) {
var self = this;
this.init = function() {
self.name = name;
};
this.init();
});
// instantiate a child object:
var baby = new Baby('Jimmy');
// call the inherited method:
baby.getName(); // undefined!
方法 getName
被正确继承,但它无法访问self.name
成员。
如果它在子类实例的范围内可用,那又如何呢?
使用 alert(baby.getName)
检查方法的主体,我得到:
function () {
alert("self.name = " + self.name);
}
这正是我所期望的。
但为什么它没有在本地解析 self
变量呢?
I am studying the accessibility to variables from methods inherited from a parent to a child object.
The inheritance is performed associating an instance of the parent to the prototype of the child's constructor:
Function.prototype.extend = function(child) {
var prototype = new this;
child.prototype = prototype;
return child;
};
So I define a parent class:
var Parent = function() {
var self = this;
this.init = function() {
self.name = 'Abram';
};
this.getName = function() {
alert('self.name = ' + self.name);
};
this.init();
};
and a child class:
var Baby = Parent.extend(function(name) {
var self = this;
this.init = function() {
self.name = name;
};
this.init();
});
// instantiate a child object:
var baby = new Baby('Jimmy');
// call the inherited method:
baby.getName(); // undefined!
The method getName
is correctly inherited, but it has no access to the self.name
member.
How come, if it is available in the scope of the child class instance?
Checking the body of the method with alert(baby.getName)
I get:
function () {
alert("self.name = " + self.name);
}
which is exactly what I expected.
But why it does not resolve the self
variable locally?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
父“getName()”函数中的变量“self”引用父实例。子对象中的“init”函数设置该对象的名称,而不是原型对象。
因此:
var Baby = new Baby("Jimmy");
会生成一个“name”属性设置为字符串“Jimmy”的对象baby.getName();
调用原型对象上的函数,因为“baby”对象上没有“getName”属性。在该函数内部,引用了变量“self”,它引用了该原型对象,而不是对象“baby”。原型对象上没有设置“名称”属性。看看如果将原型中的
.getName()
函数更改为使用this.name
而不是self.name
会发生什么。The variable "self" in the parent "getName()" function refers to the parent instance. The "init" function in the child sets the name on that object, not the prototype object.
Thus:
var baby = new Baby("Jimmy");
results in an object with a "name" property set to the string "Jimmy"baby.getName();
calls the function on the prototype object, since there's no "getName" property on the "baby" object. Inside that function, a reference is made to the variable "self", which refers to that prototype object — not to the object "baby". There's no "name" property set on the prototype object.See what happens if you change the
.getName()
function in the prototype to usethis.name
instead ofself.name
.