Struggling with modules #5402
Replies: 1 comment
-
Posted at 2016-08-12 by @allObjects @dwallersv, you ARE a bright person... I just added some lengthy explanation to the modules in modules? conversation... hopefully much better laid out than before... (So where is the issue? with the Author or Reader? ...it's up to everyone's own - jail free - judgment...) Posted at 2016-08-12 by @allObjects A while ago I had got pull request granted by @gfwilliams to enable the modules mechanisme play nicely oo... with classes... And that is how it looks like for creating a module providing, for example, a Person 'class' (The concatenated string is the source code of the 'class' definition / constructor with prototype extensions and is part of the module code):
On upload, you still get some warning, because the uploader does not find the module in the project / sandbox or on the Web espruino.com/modules, but when it comes to execution in the How can you get rid of the warning(s)?... explained in the other conversation... Replace line 16 with these two lines that supply the module name in a variable and thus escape the parsing for modules before upload ...and that get's rid of the upload warning(s):
To work the 'proper' way of modules, place the source code into the project / sandbox / modules folder (see Espruino Web IDE setting / cog wheel in top right) as
and it works with the oo charm (upload this code and fire it with
Btw, you are not bound to the class name You can even be that brief (in console or code) - no need to hold on to the module / 'Class' object / Constructor (function)... and use the class obj / constructor function anonymously in your application context... just reference by the module name and delivered as object (class object / constructor function) by the module mechanism:
Output:
And if you need the
I'm sure you know that the module name can also be a URL pointing to a module on the World Wide Web (with related security implications)... ;-) PS: Read through related using modules in modules? conversation. Posted at 2016-08-12 by dwallersv @allObjects, thank you for the kind words -- much appreciated! Of course I'm actually an incredibly arrogant bastard that actually believes he's smarter than everyone else on the planet, but I fake some humility so that all you lesser creatures will continue to be willing to interact with me. :-) :-) :-) Posted at 2016-08-12 by @allObjects LOOOOOOOOOOL - this attitude is mandatory prereq to overcome all the hurdles... it just has its challenges with other humans! Posted at 2016-08-15 by @gfwilliams Is it actually as simple as:
You actually want |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-08-12 by dwallersv
I normally think of myself as a pretty bright person, but for the life of me I seem to be dumb as a lump of granite when it comes to the Module mechanism. What I want to do is conceptually pretty simple: Make a reusable object class. Maybe the module mechanism isn't suited for this? After half an hour looking through source code for a dozen modules or so, I couldn't find anything that looked like what I'm trying to do.
I'd like to be able to do the following... The application would be coded as follows:
And the module, myClass.js, looks like this:
This, doesn't work. After the require, Espruino seems completely unaware of any constructor function "myClass".
I can see how to work around this by adding an exported interface to create an object instance directly (this is done again and again in modules with the common 'connect' function on an exports declaration), but this is very "kludgy" to me -- isn't there any way to simply include something that acts like a traditional library?
Also, I find that when I do this, it results in a doubling of space usage. The module containing all the method functions gets cached (one copy of everything), then all these functions get created again when constructed in the initial call. To make it clearer, here's what I've done to make this work:
Then, I use the module like this and it works:
Problem #1 is it uses 2x the space. I think this is because the call above caches the modules code as part of the 'require' call, and then the init() loads another copy of the class (constructor and prototype functions) again in memory. #2: I don't like it. Awkward.
#1 is the real problem. I can live with #2.
Is there some way to expose the function created when the module is loaded and executed in the root scope, as described in the modules page? This would address both #1 and #2.
Beta Was this translation helpful? Give feedback.
All reactions