<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
/***************** 静态属性 *****************/
//静态方法的特点就是可以直接使用构造函数来调用
function A() {
this.age = 10; //公开的非静态的方法
A.sex = '男'; //定义静态属性
A.PI = 3.141592653; //定义静态属性
A.max = function(){ //定义静态方法
console.log(123);
};
}
var a = new A(); //必须调用一下A函数
console.log(A.sex);
A.max();
console.log(A.PI);
/***************** 私有属性 *****************/
function B(){
this.age = 10; //公开属性
var sex = '男'; //私有属性
this.say = function(){
console.log('我是' + sex + '的');
}
}
var b = new B();
console.log(b.age); //结果:10
console.log(b.sex); //结果:undefined
b.say();
</script>
</body>
</html>
代码-JS之静态属性和私有属性
最新推荐文章于 2024-03-24 13:38:01 发布