- 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++第3版
- 零基礎搭建量化投資系統:以Python為工具
- Interactive Data Visualization with Python
- 深入淺出Windows API程序設計:編程基礎篇
- Reactive Programming With Java 9
- RISC-V體系結構編程與實踐(第2版)
- Mastering openFrameworks:Creative Coding Demystified
- Python編程:從入門到實踐(第3版)
- Java程序設計與項目案例教程
- Java Web應用開發給力起飛
- 深入實踐DDD:以DSL驅動復雜軟件開發
- Drupal 8 Development Cookbook(Second Edition)
- Learning Cocos2d-JS Game Development
- 例說FPGA:可直接用于工程項目的第一手經驗
- 用Python動手學統計學