Javascript 如何从父级调用原型子方法
我想从prototype对象调用子对象方法:Javascript 如何从父级调用原型子方法,javascript,prototype,Javascript,Prototype,我想从prototype对象调用子对象方法: function Hamster() { this.sleep = function(){ console.log('sleep'); } } Hamster.prototype = new function(){ var _self = this; this.test = function(){ this.sleep(); //_self.sleep(); //not wor
function Hamster() {
this.sleep = function(){
console.log('sleep');
}
}
Hamster.prototype = new function(){
var _self = this;
this.test = function(){
this.sleep();
//_self.sleep(); //not working
}
}
lazy = new Hamster();
lazy.test();
为什么当我使用
this.sleep()
时一切正常,但当我使用\u self.sleep()
时(\u self
变量有this
值),我会出错?为什么\u self
和test()
函数中的这个引用不一样?存储在self
中的这个引用与分配给test()
的匿名函数中调用的这个引用不同。关键是您正在创建并分配给仓鼠的这个函数范围。prototype
是一个空对象。这是因为您使用了new
关键字
Hamster.prototype = new function(){
//Stores the reference to the empty object that's the scope of the anonymous
//function because it was called using the new keyword.
var _self = this;
this.test = function(){
//Works because the this keyword is a reference to Hamster.
this.sleep();
//Does not work because the empty object doesn't have a sleep method.
//_self.sleep();
}
}
存储在self
中的this
引用与分配给test()
的匿名函数中调用的this
引用不同。关键是您正在创建并分配给仓鼠的这个函数范围。prototype
是一个空对象。这是因为您使用了new
关键字
Hamster.prototype = new function(){
//Stores the reference to the empty object that's the scope of the anonymous
//function because it was called using the new keyword.
var _self = this;
this.test = function(){
//Works because the this keyword is a reference to Hamster.
this.sleep();
//Does not work because the empty object doesn't have a sleep method.
//_self.sleep();
}
}
存储在self
中的this
引用与分配给test()
的匿名函数中调用的this
引用不同。关键是您正在创建并分配给仓鼠的这个函数范围。prototype
是一个空对象。这是因为您使用了new
关键字
Hamster.prototype = new function(){
//Stores the reference to the empty object that's the scope of the anonymous
//function because it was called using the new keyword.
var _self = this;
this.test = function(){
//Works because the this keyword is a reference to Hamster.
this.sleep();
//Does not work because the empty object doesn't have a sleep method.
//_self.sleep();
}
}
存储在self
中的this
引用与分配给test()
的匿名函数中调用的this
引用不同。关键是您正在创建并分配给仓鼠的这个函数范围。prototype
是一个空对象。这是因为您使用了new
关键字
Hamster.prototype = new function(){
//Stores the reference to the empty object that's the scope of the anonymous
//function because it was called using the new keyword.
var _self = this;
this.test = function(){
//Works because the this keyword is a reference to Hamster.
this.sleep();
//Does not work because the empty object doesn't have a sleep method.
//_self.sleep();
}
}
_self是您定义的匿名函数外部的局部变量,您没有传递它,因此它没有在内部定义\u self是您定义的匿名函数外部的局部变量,您没有传递它,因此它没有在内部定义\u self是您定义的匿名函数外部的局部变量,您确实这样做了未传递它,因此它未在内部定义\u self是您定义的匿名函数之外的局部变量,您未传递它,因此它未在内部定义您对原型的工作方式感到困惑
你可能只想
Hamster.prototype = {
test: function() {
this.sleep();
}
};
您正在做的是为原型创建一个新对象。您正在设置的\u self
变量不是指向单个仓鼠对象的this
,而是指向您正在创建的新原型对象的this
,该对象已丢失给世界。您对原型的工作方式感到困惑
你可能只想
Hamster.prototype = {
test: function() {
this.sleep();
}
};
您正在做的是为原型创建一个新对象。您正在设置的\u self
变量不是指向单个仓鼠对象的this
,而是指向您正在创建的新原型对象的this
,该对象已丢失给世界。您对原型的工作方式感到困惑
你可能只想
Hamster.prototype = {
test: function() {
this.sleep();
}
};
您正在做的是为原型创建一个新对象。您正在设置的\u self
变量不是指向单个仓鼠对象的this
,而是指向您正在创建的新原型对象的this
,该对象已丢失给世界。您对原型的工作方式感到困惑
你可能只想
Hamster.prototype = {
test: function() {
this.sleep();
}
};
您正在做的是为原型创建一个新对象。您正在设置的\u self
变量并不是指向单个仓鼠对象的this
,而是指向您正在创建的新原型对象的this
,该对象已丢失给世界。让我们用一个函数来编写它,该函数返回这两个对象,让我们更容易地比较它们
function Constructor() {}
Constructor.prototype = new function () {
var foo = this;
this.bar = function () {
return {"this": this, "foo": foo};
};
};
var instance = new Constructor(),
o = instance.bar();
现在测试一下我们得到了什么
o.this === instance; // true
o.foo === instance; // fase
// so what is o.foo ?
o.foo === Constructor.prototype; // true
这告诉我们什么
- 当我们从
实例
调用方法时,此
引用该实例
- 当我们用
new
调用一个函数时,那么这个
引用一个由new
创建的新对象实例;在本例中,它是我们随后设置为Constructor
这些参考资料是什么时候设置的
instance.bar中的this
仅在实际调用instance.bar
时设置
- 来自
new function(){…}
的this
在创建时立即设置,之后不会更改(您基本上使用了一个IIFE),它也是new
返回的内容,因此在本例中构造函数.prototype
设置的内容
让我们用一个函数来编写它,该函数返回这两个东西,让我们更容易比较它们
function Constructor() {}
Constructor.prototype = new function () {
var foo = this;
this.bar = function () {
return {"this": this, "foo": foo};
};
};
var instance = new Constructor(),
o = instance.bar();
现在测试一下我们得到了什么
o.this === instance; // true
o.foo === instance; // fase
// so what is o.foo ?
o.foo === Constructor.prototype; // true
这告诉我们什么
- 当我们从
实例
调用方法时,此
引用该实例
- 当我们用
new
调用一个函数时,那么这个
引用一个由new
创建的新对象实例;在本例中,它是我们随后设置为Constructor
这些参考资料是什么时候设置的
instance.bar中的this
仅在实际调用instance.bar
时设置
- 来自
new function(){…}
的this
在创建时立即设置,之后不会更改(您基本上使用了IIFE),它也是new
返回的内容,因此