js面向对象写法及栈的实现

 1 function Stack() {
 2   this.dataStore = [];
 3   this.top = 0; //指向栈顶的位置
 4   this.push = push;
 5   this.pop = pop;
 6   this.peek = peek;
 7   this.clear = clear;
 8   this.length = length;
 9   
10   function push(element) {
11     this.dataStore[this.top++] = element; //先赋值后++
12   }
13   
14   function peek() {
15     return this.dataStore[this.top - 1];
16   }
17 
18   function pop() {
19     return this.dataStore[--this.top]; //先--运算
20   }
21 
22   function clear() {
23     this.top = 0;
24   }
25 
26   function length() {
27     return this.top;
28   }
29 }
30 
31 var s = new Stack(); 
32 s.push("David"); 
33 s.push("Raymond"); 
34 s.push("Bryan"); 
35 console.log(s);
36 console.log("length: " + s.length()); 

 

 
注意:
这样的一步操作arr[i++]="ddd";----》这里的执行顺序是:先执行赋值后进行的++运算
等价于下面的两布操作:
arr[i] = "ddd";
i++;
 
 
//这样写才是先执行++运算
arr[++i]="ddd";

转载于:https://www.cnblogs.com/dongruiha/p/6307318.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值