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

JavaScript:如何使用实例变量

JavaScript:如何使用实例变量,javascript,oop,Javascript,Oop,为什么这不能像预期的那样起作用。(见预期评论) 更新:相应地修复了jAndy的一些建议此处的多个错误: 您不会将DoStuff作为模块接口返回 instance\u var未声明,可能意味着public\u instance\u var doOtherStuff从未分配给模块,只需像doOtherStuff()那样调用它即可 固定代码: var Module = function () { var public_instance_var; function doStuff(

为什么这不能像预期的那样起作用。(见预期评论)

更新:相应地修复了jAndy的一些建议

此处的多个错误:

  • 您不会将
    DoStuff
    作为模块接口返回
  • instance\u var
    未声明,可能意味着
    public\u instance\u var
  • doOtherStuff
    从未分配给
    模块
    ,只需像
    doOtherStuff()那样调用它即可
固定代码:

var Module = function () {
    var public_instance_var;

    function doStuff() {
        doOtherStuff();
        console.log(public_instance_var); // expected: true, but logs undefined
    };

    function doOtherStuff() {
        public_instance_var = true;
    };

    return {
        doStuff: doStuff,
        public_instance_var: public_instance_var
    }
}();

Module.doStuff();

像这样更改代码

var Module = function () {
    var public_instance_var;

    function doStuff () {
        doOtherStuff();
        console.log("var is ", public_instance_var); // expected: true, but logs undefined
    };

    function doOtherStuff() {
        public_instance_var = true;
    };

    return {
        public_instance_var: public_instance_var,
        doStuff : doStuff
    }
}();

Module.doStuff();
  • 您必须返回
    doStuff()
    函数(否则函数外部将未定义)和
    public\u instance\u var
    而不是
    instance\u var
  • 您需要执行
    doOtherStuff()
    而不带前缀
    模块。

    • 简单地说,这段代码的作用是:创建并运行一个函数,并将其返回值赋给一个变量:
      模块
      。返回值是一个具有1个属性的对象:
      public\u-instance\u-var
      ,该属性指向变量
      instance\u-var
      ,或者(在更正输入错误后:
      public\u-instance\u-var
      )。此变量已声明,但未实例化。因此,返回值如下所示:

      Module.public_instance_var = undefined
      
      var Module = (function()
          var public_instance_var;
          function doStuff () {
              this.doOtherStuff();
              console.log(public_instance_var); // expected: true, but logs undefined
          };
          function doOtherStuff() {
              public_instance_var = true;
          };
          return {
              public_instance_var: public_instance_var,
              doStuff: doStuff,
              doOtherStuff: doOtherStuff
          };
      })();
      
      最后一行
      Module.doStuff()一点也不起作用:模块是一个没有方法的对象。当匿名函数返回时,您声明的函数将被垃圾收集。如果希望访问这些函数,则需要在return语句中包含它们。通读闭包、对象构造函数和设计模式,但我想说的是,您所关注的代码如下所示:

      Module.public_instance_var = undefined
      
      var Module = (function()
          var public_instance_var;
          function doStuff () {
              this.doOtherStuff();
              console.log(public_instance_var); // expected: true, but logs undefined
          };
          function doOtherStuff() {
              public_instance_var = true;
          };
          return {
              public_instance_var: public_instance_var,
              doStuff: doStuff,
              doOtherStuff: doOtherStuff
          };
      })();
      
      当然,这样你的变量
      public\u instance\u var
      就是一个公共属性,所以我猜你真正想做的就是模拟一个私有属性和方法。在这种情况下,您可能会得到与以下类似的代码:

      var Module = (function()
      {
          var public_instance_var;
          return {
              //public_instance_var: public_instance_var, remove this line
              //the closure will preserve access to the variable
              doStuff: function ()
              {
                  this.doOtherStuff();//this, you're referencing the object's property
                  console.log('here I am');
              },
              doOtherStuff: function ()
              {
                  public_instance_var = true;
                  //this won't work anymore:
                  //this.public_instance_var = true;
              };
          }
      })();
      
      Module.doStuff()
      现在记录
      我在这里
      ,但是
      doOtherStuff
      现在也是一个公共方法。以下是您可以选择的解决问题的方法:

      var Module = (function()
      {
          var public_instance_var;
          function doOtherStuff ()
          {
              public_instance_var = true;
          };
          return {
              //public_instance_var: public_instance_var, remove this line
              //the closure will preserve access to the variable
              doStuff: function ()
              {
                  doOtherStuff();//don't use this here, but the reference to the function exists thanks to closure
                  console.log('here I am');
                  console.log(public_instance_var);//logs true
              }
          };
      })();
      

      这些只是使用闭包和返回对象的函数所能做的一些非常强大的事情
      只要读几篇文章,比如其中一篇,就有更好的。在谷歌上搜索术语
      powerconstructors

      ,你似乎错过了一个例子,返回中是否有输入错误?实例变量在哪里定义?
      Module.doStuff()
      应该抛出一个错误,即
      模块
      没有可调用属性
      doStuff
      (或类似属性)。永远不会执行包含您的注释的行。另外,这实际上与OOP.public\u instance\u var没有任何关系:instance\u var显示了一个指向私有属性public\u instance\u var的公共指针,因此在我的代码中是正确的。你其余的观察似乎是正确的。