作者:Bertram Cao
時(shí)間:2018-4-18
1.棧:后進(jìn)先出(Last-In-First-Out),只發(fā)生在棧的頂部,JS提供了push()和pop()兩個(gè)方法学赛,通過對(duì)數(shù)組的操作實(shí)現(xiàn)類似棧的行為。
2.簡單粗暴吞杭,直接上代碼:
function stack () {
this.arr = [];
this.push = push;
this.pop = pop;
this.num = num;
}
function push (value) {
this.arr.push(value);
}
function pop () {
return this.arr.pop();
}
function num () {
return this.arr;
}
var person = new stack();
person.push('daming');
person.push('xiaohong');
person.push('bertram');
console.log(person.num()); // ["daming", "xiaohong", "bertram"]
console.log(person.pop()); // bertram
console.log(person.num()); // ["daming", "xiaohong"]
構(gòu)造stack函數(shù)盏浇,下邊是定義的一系列方法,創(chuàng)建person對(duì)象芽狗,通過調(diào)用定義好的方法實(shí)現(xiàn)對(duì)棧的插入(推入)和移除(彈出)操作绢掰。
大明進(jìn),大明進(jìn)童擎,大明進(jìn)完小紅進(jìn)滴劲,小紅進(jìn),小紅進(jìn)顾复,小紅進(jìn)完自己進(jìn)班挖,自己進(jìn),自己進(jìn)芯砸,自己剛進(jìn)去就被揪出來~