-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleDemo3.js
More file actions
46 lines (43 loc) · 968 Bytes
/
ModuleDemo3.js
File metadata and controls
46 lines (43 loc) · 968 Bytes
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
var basketModule = (function(){
//私有变量
var basket = [];
function doSomethingPrivate(){
//...
}
function doSomethingElsePrivate(){
//...
}
//返回一个暴露出的公有对象
return{
//添加item到购物车
addItem:function(values){
basket.push(values);
},
//获取购物车里面的item数
getItemCount:function(){
return basket.length;
},
//私有函数的公有形式别名
doSomething:doSomethingPrivate,
//获取购物车里所有item的价格总值
getTotal:function(){
var itemCount = this.getItemCount(),
total = 0;
while (itemCount--){
total += basket[itemCount].price;
}
return total;
}
}
})();
basketModule.addItem({
item:"bread",
price:0.5
})
basketModule.addItem({
item:"butter",
price:0.3
})
console.log(basketModule.getItemCount());
console.log(basketModule.getTotal());
console.log(basketModule.basket);//未暴露值为undefined 但是已经定义不会报错 is not define