Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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
Inheritance 类方法是如何在javascript中实现的?_Inheritance_Scope_Javascript - Fatal编程技术网

Inheritance 类方法是如何在javascript中实现的?

Inheritance 类方法是如何在javascript中实现的?,inheritance,scope,javascript,Inheritance,Scope,Javascript,我正在学习一个图书馆,看到一个例子,我不明白它是如何做到的? 在类的函数中,此变量包含类的所有方法。 在外部,只有公共方法可用 受保护的方法更有趣。它们仅在继承的类中可用。 它是如何工作的 请参阅以下文档中的示例: /** * A-class */ var ClassA = AWeb.class({ public : { /** * A-class constructor */ constructor : function()

我正在学习一个图书馆,看到一个例子,我不明白它是如何做到的? 在类的函数中,此变量包含类的所有方法。 在外部,只有公共方法可用

受保护的方法更有趣。它们仅在继承的类中可用。 它是如何工作的

请参阅以下文档中的示例:

/**
  * A-class
  */
var ClassA = AWeb.class({
   public : {
      /**
        * A-class constructor
        */
      constructor : function() {
         /* Private variable */
         this.variable1 = "A";
         this.calls = 0;
      },

      /**
        * Function returns information about the object
        */
      getInfo : function() {
         this.incCalls();

         return "variable1=" + this.variable1 + ", calls=" + this.calls;
      }
   },
   protected : {
      /**
        * Protected function
        */
      changeVariable1 : function( value ) {
         this.variable1 = value;
      }
   },
   private : {
      /**
        * Private function
        */
      incCalls : function() {
         this.calls++;
      }
   }
});
/**
  * C-class
  */
var ClassC = AWeb.class({
   extends : ClassA,
   public : {
      /**
        * B-class constructor
        */
      constructor : function() {
         this.super();
         this.changeVariable1( "C" );
      },

      /**
        * Function returns extended information about the object
        */
      getLongInfo : function() {
         return this.incCalls !== undefined ? "incCalls visible" : "incCalls undefined";
      }
   }
});
/**
  * Main project function
  */
function main() {
   var a = new ClassA(),
       c = new ClassC();

   alert(
      "a instanceof ClassA: " + (a instanceof ClassA) + "\n" +
      "a instanceof ClassC: " + (a instanceof ClassC) + "\n" +

      "a.getInfo " + (a.getInfo ? "exists" : "undefined") + "\n" +
      "a.getLongInfo " + (a.getLongInfo ? "exists" : "undefined") + "\n" +
      "a.changeVariable1 " + (a.changeVariable1 ? "exists" : "undefined") + "\n" +
      "a.getInfo()=" + a.getInfo() + "\n\n" +

      "c instanceof ClassA: " + (c instanceof ClassA) + "\n" +
      "c instanceof ClassC: " + (c instanceof ClassC) + "\n" +

      "c.getInfo " + (c.getInfo ? "exists" : "undefined") + "\n" +
      "c.getLongInfo " + (c.getLongInfo ? "exists" : "undefined") + "\n" +
      "c.changeVariable1 " + (c.changeVariable1 ? "exists" : "undefined") + "\n" +

      "c.getInfo()=" + c.getInfo() + "\n" +
      "c.getLongInfo()=" + c.getLongInfo()
   );
}

如果有帮助的话:

对不起,这个问题不准确。我知道javascript中的继承原则。 我对在指定库中实现继承感兴趣。它有效,我测试过了

库是免费的,但它有一个封闭的代码,所以无法读取它。 它适用于所有浏览器

我将指定的代码包装到html页面中,请查看

<!DOCTYPE HTML>

<html>
<head>
    <title>Sample project</title>
    <!-- Styles -->
    <link rel="stylesheet" type="text/css" href="http://a-web.me/styles/default/a-web.css" >
    <!-- Scripts -->
    <script type="text/javascript" src="http://a-web.me/scripts/a-web.js"></script>
</head>
<body>
    <script type="text/javascript">
        /**
          * A-class
          */
        var ClassA = AWeb.class({
           public : {
              /**
                * A-class constructor
                */
              constructor : function() {
                 /* Private variable */
                 this.variable1 = "A";
                 this.calls = 0;
              },

              /**
                * Function returns information about the object
                */
              getInfo : function() {
                 this.incCalls();

                 return "variable1=" + this.variable1 + ", calls=" + this.calls;
              }
           },
           protected : {
              /**
                * Protected function
                */
              changeVariable1 : function( value ) {
                 this.variable1 = value;
              }
           },
           private : {
              /**
                * Private function
                */
              incCalls : function() {
                 this.calls++;
              }
           }
        });
        /**
          * C-class
          */
        var ClassC = AWeb.class({
           extends : ClassA,
           public : {
              /**
                * B-class constructor
                */
              constructor : function() {
                 this.super();
                 this.changeVariable1( "C" );
              },

              /**
                * Function returns extended information about the object
                */
              getLongInfo : function() {
                 return this.incCalls !== undefined ? "incCalls visible" : "incCalls undefined";
              }
           }
        });
        /**
          * Main project function
          */
        function main() {
           var a = new ClassA(),
               c = new ClassC();

           alert(
              "a instanceof ClassA: " + (a instanceof ClassA) + "\n" +
              "a instanceof ClassC: " + (a instanceof ClassC) + "\n" +

              "a.getInfo " + (a.getInfo ? "exists" : "undefined") + "\n" +
              "a.getLongInfo " + (a.getLongInfo ? "exists" : "undefined") + "\n" +
              "a.changeVariable1 " + (a.changeVariable1 ? "exists" : "undefined") + "\n" +
              "a.getInfo()=" + a.getInfo() + "\n\n" +

              "c instanceof ClassA: " + (c instanceof ClassA) + "\n" +
              "c instanceof ClassC: " + (c instanceof ClassC) + "\n" +

              "c.getInfo " + (c.getInfo ? "exists" : "undefined") + "\n" +
              "c.getLongInfo " + (c.getLongInfo ? "exists" : "undefined") + "\n" +
              "c.changeVariable1 " + (c.changeVariable1 ? "exists" : "undefined") + "\n" +

              "c.getInfo()=" + c.getInfo() + "\n" +
              "c.getLongInfo()=" + c.getLongInfo()
           );
        }
    </script>
</body>
</html>

请澄清你的问题。如果您对封装原理或它在JS中的工作方式感兴趣,我几乎不敢相信它能工作。要么他们是骇人听闻的黑客,要么他们只是假装而已。当我看到AWeb.class的实现时,我可能会说更多,但我无法在网上找到代码。请张贴你想解释相关部分的库本身,或者至少链接代码,而不是它的使用示例:-对于不准确的问题,抱歉。我知道javascript中的继承原则。我对在指定库中实现继承感兴趣。它有效,我测试过了。库是免费的,但它有一个封闭的代码,所以无法读取它。它适用于所有浏览器。我将指定的代码包装到html页面中,请看。有人有什么建议吗?