2/20/2023 0 Comments Css eventscripts plugin![]() A more functional approach would go a long way toward making our system safer and more predictable. That’s not uncommon in JavaScript, but it doesn’t feel great - especially when other plugins could be in there messing with the same internal state. This violates the open-closed principle, which states that a software entity should be open for extension but closed for modification.Īlso, the “squared” function works by producing side effects. If a plugin was to redefine an internal function (like setValue), it could produce unexpected results for BetaCalc and other plugins. While this is useful for getting and setting the currentValue, it’s also dangerous. Easy distribution is super important!īy giving plugins access to BetaCalc’s this, they get read/write access to all of BetaCalc’s code. This means that plugins can be downloaded via npm and imported as ES6 modules. The plugin is a simple object-literal that can be passed into our function. So now, BetaCalc has a new “squared” button, which can be called directly: tValue(3) // => 3 When the plugin is registered, the exec function is attached directly to our betaCalc object as a method, giving it access to BetaCalc’s this. In our plugin, the exec function contains our code, and the name is our metadata. Metadata (like a name, description, version number, dependencies, etc.).In many plugin systems, it’s common for plugins to have two parts: tValue(this.currentValue * this.currentValue) The job of this method is simple: take the external plugin, grab its exec function, and attach it to our calculator as a new method: // The CalculatorĪnd here’s an example plugin, which gives our calculator a “squared” button: // Define the plugin We’ll start by creating a register method that other developers can use to register a plugin with BetaCalc. We have a setValue method, which takes a number and displays it on the “screen.” We also have plus and minus methods, which will perform an operation on the currently displayed value. The calculator works by printing its result via console.log.įunctionality is really limited right now. We’re defining our calculator as an object-literal to keep things simple. Here’s some basic code to get us started: // The Calculator The goal for BetaCalc is to be a minimalist JavaScript calculator that other developers can add “buttons” to. Let’s start with an example project called BetaCalc. I’m using the word “plugin” but these things are sometimes called other names, like “extensions,” “add-ons,” or “modules.” Whatever you call them, the concept (and benefit) is the same. So how do you go about building a plugin system? Let’s answer that question by building one of our own, in JavaScript. This makes the core project more valuable, and it builds a community - all without creating an additional maintenance burden. Plugins are a common feature of libraries and frameworks, and for a good reason: they allow developers to add functionality, in a safe, scalable way. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |