官术网_书友最值得收藏!

Using the stack

To use the Stack class created previously, you would have to make a minor change to allow the stack to be used based on the environment in which you are planning to use it. Making this change generic is fairly straightforward; that way, you do not need to worry about multiple environments to support and can avoid repetitive code in each application:

// AMD
if (typeof define === 'function' && define.amd) {

define(function () { return Stack; });

// NodeJS/CommonJS

} else if (typeof exports === 'object') {

if (typeof module === 'object' && typeof module.exports ===
'object') {

exports = module.exports = Stack;
}

// Browser

} else {

window.Stack = Stack;
}

Once we add this logic to the stack, it is multi-environment ready. For the purpose of simplicity and brevity, we will not add it everywhere we see the stack; however, in general, it's a good thing to have in your code.

If your technology stack comprises ES5, then you need to transpile the preceding stack code to ES5. This is not a problem, as there are a plethora of options available online to transpile code from ES6 to ES5.
主站蜘蛛池模板: 年辖:市辖区| 中牟县| 屯昌县| 安庆市| 永安市| 民勤县| 嘉祥县| 新乐市| 台山市| 潞城市| 乃东县| 郁南县| 海安县| 潼关县| 密云县| 穆棱市| 嘉兴市| 筠连县| 富民县| 嘉义市| 淅川县| 璧山县| 司法| 塔城市| 保定市| 武邑县| 寻甸| 高雄县| 横山县| 绥德县| 泌阳县| 康马县| 开封市| 安吉县| 曲麻莱县| 新沂市| 海林市| 夏津县| 河间市| 高州市| 萝北县|