习惯Lua用法,如何在Embed模块无需传参访问client变量?
Hey there! Since you're used to working with Lua, I get wanting a clean way for your Embed module to access the client variable without passing it around everywhere. Let's walk through a few solid JavaScript approaches to make this happen, based on your file structure (Client.js, Embed.js, client.js):
If you don't mind a bit of global scope usage, you can attach the client to the global object (Node.js uses global, browsers use window):
// client.js var EmbedManager = require('./Embed'); var client = require('./Client'); // Mount client to global scope (Node.js environment) global.client = client; new EmbedManager().init().output();
Then in your Embed.js, you can access client directly:
// Embed.js function EmbedManager() { this.init = function() { // Access the global client directly console.log('Successfully accessed client:', client); return this; }; this.output = function() { // Use client for your operations client.sendEmbed(); }; } module.exports = EmbedManager;
Note: This is simple but can lead to global variable pollution, so it's not ideal for large-scale projects.
A cleaner approach is to inject the client into the Embed module explicitly, which keeps dependencies clear:
// client.js var Embed = require('./Embed'); var client = require('./Client'); // Pass client to Embed's factory function var EmbedManager = Embed.createManager(client); new EmbedManager().init().output();
Update Embed.js to accept the client via a factory:
// Embed.js function EmbedManager(client) { // Store client internally this._client = client; this.init = function() { console.log('Accessed client:', this._client); return this; }; this.output = function() { this._client.renderEmbed(); }; } // Export a factory to inject client module.exports = { createManager: function(client) { return function() { return new EmbedManager(client); }; } };
This method is modular, avoids global scope bloat, and makes the dependency between Embed and client explicit—great for maintainable code.
If you want to keep the client reference private within the Embed module, use a closure to store it:
// client.js var Embed = require('./Embed'); var client = require('./Client'); // Set the client in Embed module first Embed.setClient(client); var EmbedManager = new Embed(); EmbedManager.init().output();
Embed.js implementation with closure:
// Embed.js // Private variable stored in closure scope var _internalClient; function EmbedManager() { this.init = function() { console.log('Accessed client:', _internalClient); return this; }; this.output = function() { _internalClient.publishEmbed(); }; } // Export a wrapped interface to set client and get manager module.exports = (function() { var managerInstance = new EmbedManager(); return { setClient: function(client) { _internalClient = client; }, init: function() { return managerInstance; } }; })();
This keeps the client reference hidden from external code, while still letting your EmbedManager instance access it without explicit parameters.
内容的提问来源于stack exchange,提问作者The Doctor

