【Q】在使用class定义类的时候,由于在代码中使用了(function(){})自调用方法,没有先声明class类,出现了报错问题,代码如下:
报错:Cannot access 'Stack' before initialization
(function() {
console.log(clacExp(["4", "13", "5", "/", "+"]));
})();
class Stack {
constructor() {
this._items = [];
}
push(item) {
this._items.push(item);
}
pop() {
return this._items.pop();
}
peek() {
return this._items[this._items.length-1];
}
isEmpty() {
return !this._items.length;
}
size() {
return this._items.length;
}
clear() {
this._items = [];
}
}
function isOperator(str) {
return ["+", "-", "*", "/"].includes(str);
}
function clacExp(exp) {
let stack = new Stack();
let current_str = null,
expression1 = null,
expression2 = null,
exp_str = null,
res