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

标签 inheritance scope javascript

我正在学习一个库,看到一个示例,但我不明白它是如何完成的? 在类的函数中,“this”变量包含该类的所有方法。 外部只有公共(public)方法可用。

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

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

/**
  * 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()
   );
}

如果有帮助:http://a-web.me/tutorial_javascript.html

最佳答案

很抱歉这个问题不准确。我知道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>

关于inheritance - 类方法在javascript中是如何实现的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18817066/

相关文章:

scala - 如何将隐式类参数映射到特征变量?

java - Java中可以不使用abstract关键字来定义抽象方法吗?

java - 无法从扩展类获取属性值

java - 什么使这个上下文静态?怎么办?

javascript - 如何隐藏超链接下划线并在 javascripts 中保持原始字体颜色?

javascript - 如何从另一个域加载cookie

JavaScript Object.create——继承嵌套属性

c++ - 使用辅助方法操作字符串的范围问题

ruby-on-rails - 如何在复杂的 Rails has_many 作用域中进行外部连接?

javascript - 使用应用程序组件的 HTML 的 Angular 库