电商网站推广渠道,登封网络推广哪家好,网站建设需求调查问卷,建立外贸网站多少钱栈的概念就不再赘述#xff0c;无可厚非的先进后出#xff0c;而JS又是高级语言#xff0c;数组中的方法十分丰富#xff0c;已经自带了push pop方法进行入栈出栈的操作。
1.基本实现
class Stack {constructor() {this.items [];}// 入栈push(item) {this.items.push(i…栈的概念就不再赘述无可厚非的先进后出而JS又是高级语言数组中的方法十分丰富已经自带了push pop方法进行入栈出栈的操作。
1.基本实现
class Stack {constructor() {this.items [];}// 入栈push(item) {this.items.push(item);}// 出栈pop() {return this.items.pop();}// 栈顶元素peek() {return this.items[this.items.length - 1];}// 栈的大小size() {return this.items.length;}// 清空栈clear() {this.items [];}// 获取栈内元素getItem() {return this.items;}// 判断栈是否为空isEmpty() {return this.items.length 0;}
}代码很简单重要的是思想 先进后出先进后出!! 2.函数执行模式——栈 计算机在调用函数的时候采用的是函数栈的形式用于递归 如
const fun1 ()
{return console.log(1)
}
const fun2 ()
{fun1()return console.log(2)
}
fun2()那么结果应该是什么呢答案是 1 2 可以证明 先执行了fun1(后执行了fun2() 那么原理呢原理是在调用函数时采用的是栈的形式在调用fun2时 fun2入栈然后fun1入栈所以在执行时为fun1先出栈fun2后出栈