Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.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_Prototype - Fatal编程技术网

Javascript 如何从父级调用原型子方法

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

我想从prototype对象调用子对象方法:

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
    返回的内容,因此