类方法如何在 javascript 中实现
How the class methods are implemented in javascript?
我正在学习一个库,看到一个例子,我不明白它是怎么做到的?在类的函数中,"this"变量包含类的所有方法。在外面,只有公共方法可用。
受保护的方法更有趣。它们仅在继承的类中可用。它是如何工作的?
请参阅以下文档中的示例:
/**
* 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>
相关文章:
- 绑定函数时在IE7中未实现Javascript错误
- 可以't实现JavaScript翻转计数器
- 如何实现 Javascript 自动完成功能
- 实现JavaScript内部方法的源代码
- 当实现javascript倒计时时,其他html元素将消失
- 在aspx页面中实现javascript时出现问题
- 这是实现javascript字符串长度属性的正确方法吗
- 将递归函数转换为异步 CPS 实现 (javascript)
- 如何在Wordpress中实现javascript
- 在jQuery方法中实现Javascript变量
- 无法实现 JavaScript 代码
- 在 Rails 应用程序中实现 JavaScript 库
- 如何实现 JavaScript 对象继承
- 我将如何实现JavaScript
- 当浏览器开始实现JavaScript时,如何在支持一两个版本较旧的浏览器的同时开始使用它们
- 有没有其他方法可以在纯python中实现javascript的onClick()函数
- 如何在JavaCC中实现JavaScript自动分号插入
- 响应式网页设计 - 如何实现JavaScript部分
- 用Selfish实现Javascript中的简单继承
- 如何实现JavaScript/ECMAScript“;这里没有线路终止器”;JavaCC中的规则