静态方法与实例方法

JavaScript中的静态方法实例方法Java几乎相同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>静态方法和实例方法</title>
<script
src="https://code.jquery.com/jquery-1.12.4.js"
integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU="
crossorigin="anonymous">
</script>
<script>
//创建一个 AClass 对象
function AClass() {
}

//添加静态方法
AClass.staticMethod = function () {
alert("staticMethod");
};
//执行静态方法
AClass.staticMethod();
//添加实例方法
AClass.prototype.instanceMethod = function () {
alert("instanceMethod");
};
//创建 AClass 实例
var a = new AClass();
//调用 a 的实例方法
a.instanceMethod();
</script>
</head>
<body>

</body>
</html>

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×