-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathooJs.js
73 lines (66 loc) · 2.07 KB
/
ooJs.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* ************************************************************************************************
* 前端代码在线测试工具 https://jsbin.com/paximakoso/edit?js,output
*
* JavaScript面向对象编程
*
* ************************************************************************************************ */
/**
* 给User对象设置属性
* @param properties
* @constructor
*/
(function () {
function User(properties) {
for (let i in properties) {
(function (which) {
let p = i;
which["get" + p] = function () {
return properties[p];
};
which["set" + p] = function (val) {
properties[p] = val;
};
})(this);
}
/**
* 私有方法,只能在函数内部使用,无法在函数外部调用
*/
function privateMethod() {
console.info("call privateMethod");
}
/**
* 特权方法,有权方法私有变量、私有函数的公有方法
* */
this.display = function () {
console.info("call display.");
privateMethod();
};
}
/**
* 公共方法,在原型上添加
*/
User.prototype.say = function () {
console.info("call say");
};
User.cloneUser = function (user) {
return new User(user.getname(), user.getage());
};
/* ************************************************************************************
* 测试
* ************************************************************************************/
function test01(user) {
console.info(user.name);
console.info(user.getname() === "pengpeng");
user.setname("pp");
console.info(user.getname());
user.display();
user.say();
}
const user = new User({
name: "pengpeng",
age: "31"
});
test01(user);
const user2 = User.cloneUser(user);
test01(user2);// 调用时 user2.getname() 方法报错 原型继承
})(window);