87,990
社区成员
发帖
与我相关
我的任务
分享
<script>
function stack(){
this.data='';
this.next=null;
this.push=function(d){
var tmp=new stack()
tmp.data=d;
tmp.next=this.next;
this.next=tmp;
}
this.pop=function(){
var tmp=new stack()
tmp=this.next;
this.next=tmp.next;
return tmp.data;
}
}
var s=new stack();
s.push("10");
s.next.data='111'; //问题在这里,我不希望用户可以这样直接访问类的next或data属性。
s.push("20");
s.push("30");
alert(s.pop());
alert(s.pop());
alert(s.pop());
</script>
<script>
function stack(){
var next=null;
this.push=function(d){
var tmp=new stack()
tmp.data=d;
tmp.next=next;
next=tmp;
}
this.pop=function(){
var tmp=new stack()
tmp=next;
next=tmp.next;
return tmp.data;
}
}
var s=new stack();
s.push("10");
alert(s.next);
s.push("20");
s.push("30");
alert(s.pop());
alert(s.pop());
alert(s.pop());
</script>