- Hands-On Data Structures and Algorithms with JavaScript
- Kashyap Mukkamala
- 199字
- 2021-06-30 19:12:08
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.
推薦閱讀
- Visual C++程序設計學習筆記
- 軟件項目管理(第2版)
- The Android Game Developer's Handbook
- Apache Spark 2 for Beginners
- 區塊鏈:以太坊DApp開發實戰
- Learning ArcGIS Pro
- C語言程序設計案例式教程
- C語言程序設計
- SharePoint Development with the SharePoint Framework
- 網站構建技術
- iPhone應用開發從入門到精通
- FFmpeg開發實戰:從零基礎到短視頻上線
- Learning Python Data Visualization
- After Effects CC技術大全
- C# 7.0本質論