Assigning function to prototype member results in Uncaught ReferenceError #1187
Replies: 15 comments
-
Posted at 2018-09-08 by @allObjects Are you trying this all in the console? I'm not sure about the context and intentions... but these conversations may shed some light on what's about with modules in addition to: espruino.com/Writing+Modules: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-08 by Robin Sat 2018.09.08
Thanks for the quick response @allObjects and I'm reviewing the additional links now. Just trying to test a simple module with cut-n-paste into the editor window right-pane. Following the instructions at Writing+Modules, wrapping the code in I stripped out all non-essential lines and have it down to the fifteen lines above. Just performing a send creates the error. Simple concepts taken from and modeled after:
At this point, I'm just trying to get something, . . . anything to work.
For clarification for me, technically, as the code has just been sent to the device, isn't it actually running, and therefore we see what I presented above, typing 'this' into the left-hand pane console? My point is, the IDE is aware of the function, otherwise it couldn't possibly know about it otherwise in order to produce the error. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-08 by @allObjects
yes, but more things happen and the context is different then when Also, when a module is uploaded, the variable I noticed
I assume you are not trying to send the module, do you? You send only your application code - that uses your modules - to Espruino board in the IDE. The module you store in the modules folder of your local sandbox. The use of the module in your application code is detected by the upload function of the IDE on the presence of Your application code using - for example, the GrovRelay - looks something like this:
After upload, Espruino has a (global) variable Now you go back to your modules, and they will work... Stick this code into your modules folder:
Then upload the following application code:
...and you have your elaborate ;-) blinking example... blink frequency: 1Hz, duty cycle: 50% (should it not work... then there may be a typo... code proof read but not executed on Espruino yet...) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-08 by Robin Sat 2018.09.08 Thank you for the detailed explanation. That content confirms what I thought I understood.
I'm following this pp at: "To test out your module, we'd suggest that you copy it verbatim into the right-hand side of the Web IDE, with the line var exports={}; at the top, and at the bottom, where you use the module, write exports.myfunction() If I understand the above correctly, I placed those fifteen lines of code between, and was able to create one instance. It was when I attempted to create the accessor, the error occurred. Don't have much time this evening, but I'll try your code snippets as you suggest and, as the above pp suggests. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-09 by @allObjects ...Ic what you mean you follow... As you have seen the example of module and the example of use in my previous post, I do now what the instructions for test say. In a nutshell, it is emulating the way the module gets accessed. All code, application AND module are in the same code that is uploaded. Here it goes.
My style is always getting the module into a variable, and then using it from there, since it is a class or some object... more the object-oriented (oo) way. Most example do the other way - for various but not needed reasons: They do the require(... and cascade the function or method all on it right away... more... more the function - and JS basic - way. Separating helps with testing... less code is different from final way. Your first example is kind of a mix of both and messes with your exports anyway. So decide for one. The functional approach is like the example describes an your second approach in your first example, where you expose a plain function that calls the new on the class method and return an object. Most modules work this way because there is a connect(... required that creates and returns the connection object through which you access the periphery / sensor / display. Initially, assigning a constructor as class was not working... and since even my first Espruino tries were using a class (constructor funtion) rather than a plain function that hides oo. The thing returned from requre(... did not understand new (Espruino intrepreter's new could not apply new on an object returned from require(...). I asked then @gordon to remove the restriction/enable exports to be a class (constructor function) and he changed Espruino right away. Now more and more modules show as classes. It is kind of more natural, because you do not want to think that you are talking to a connection object, which is a technical thing that hides the real object. You want to talk to the sensor object or what directly... so the class that is built models the sensor... You can read up in these conversations. That all started 4+ years ago:
Until the oo/class-way was available, I had to build the module a bit funny: esxport a function that returned me the class / constructor function... and it worked but required extra code, like I demonstrate here - module code and application code (as final): Module code:
Application code:
Functionally, there is no real difference... but the oo way looks more straight forward - at least to me, especially when you can have multiple instances of something like a sensor, testLED, etc. (Some modules are built the way that multiple instances cannot exist, because not all instance variables are in the object, but rather just in the module on level . They survive garbage collect only because of being referenced directly or indirectly by exposed variables (functions / data objects), which is by the way how to create private state and behavior in JavaScrpt. So watch out for that... it is the same thing with hidden initializations, which should not happen on load of the module but on construction of the instance object. Last paragraphs deal with things not really of your concern at hand and I hope it did not confuse more - or only temporarily a bit - but in the end are useful to know and apply to end up with better systems. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-09 by @gfwilliams I'm on holiday this week so only quickly glanced at this, but the error shows the line In this case So... I'm not sure what's going on here - in this case (and in previous posts) you've shown code that's been running on your device that you don't think you've uploaded. I guess it's possible you've been flipping between upload styles (save on send, or normal) and have two copies of your code in there? Can you try running |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-09 by Robin Sun 2018.09.09
Not sure what you mean here: 'don't think you've uploaded' after send, I check the left-hand console for ack. Sometimes the errors are there, then coding progress ceases. If I can get a successful send, I keep going. or, did you mean the reference to when the green progress bar never completes and the IDE freezes, forcing a close re-launch? In that case I know it hasn't uploaded and I'm forced to power down the Pico as well, memory clears, so that Windows can remove and recognize the Com Port on IDE launch.
That is the error I see with the code I posted. Per suggestion, I started with a larger file and kept paring until the least possible amount of code that still caused the error remained.
No, I'm not and haven't made any changes to settings. It has the defaults from the install. I'm making every attempt not to introduce any other activity that could get in the way. EVERY time I, reset(1), load file if needed, block copy code into the right-hand side editor, edit until syntax checker agrees, then send. Look for Ack. I'll try Could the Ctrl+C Ctrl+V process be introducing garbage chars? In addition, I'm now attempting using notepad.exe and notepad++.exe to see if the editors use a different mechanism. When I block highlight Ctrl+C code in the right-hand editor, could garbage chars be copied to the clipboard? Could the clipboard retain garbage chars? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-09 by @allObjects For me, things do still not add up... ...and made me conclude that this is the one reason - co-concluding w/ @gordon:
indeed... :[ - because if the IDE settings are installation default with sandbox set, then the following application code uploads just fine - because there is no issue in the module - and it runs as expected (after issuing Module code (as in initially posted by @robin in post #1 w/ semicolon (;) removed from
Application code:
Console output as expected:
Above application code uses the method mode (I call it this way). It works because you added
And guess what, it woks, as console output shows:
***Just make sure that precedence to the Espruino interpreter with parentheses to get the module first and then apply 'new' to it - object return by it . *** Also be aware that now an array with a pin (as first element) has to be passed in the constructor function with new to allow the constructor to work (and the upload not to fail, because - as said again - construction is executed on upload, since code is in level 0... and [sic: now rambling] I'm saying here again as said in many other places, that executing application code in Level 0 is 'bad' / calls for troubles, because it is executed and creates all kinds of havoc, timing, things not initialized when running disconnected, etc... therefore, the application code again in a more safe way, with execution of application code in onInit(){...} or triggered in onInit()) :
And example of how to test the module inline as instructed by espruino.com/Writing+Modules:
NB: I'm PICO, and I approve this post! ...eh: code! ;-) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-09 by Robin Sun 2018.09.09 Thank you @allObjects for making an extra ordinary effort in completing this and welcome the world of Pico. I used your snippets verbatim, here's the output, after I discovered there are two 'modules' folders beneath the installation _sbx folder
There is definitely something different between this IDE on this Windows platform than that which you guys have. Would love to know what is going on. But we are getting closer . . . Do I need to change the Settings >> Project folder to point to the 'modules' folder?
EDIT The IDE won't allow me to place the insertion caret cursor in the URL field of Settings >> Project So, I made copies and placed in the \projects\modules the \modules and the \projects folders, but get the same results. I restated the IDE and now get a *Module testLED not found* error. Under Settings >> Communications > "Where to search online for modules when `require()` is used" > http://www.espruino.com/modules as the module isn't at the remote URL while we are online, does this need to be changed for the local drive? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-09 by @allObjects @robin, that second folder could come from once using the project folder in the already existing sandbox folder again as sandbox folder in Espruino IDE settings... or when saving your code, application or module,... (this comes from some convenience that folders get created when not present... without telling the user... but thats a different ball game). I doubt that there is a difference in the IDE in that respect. PS: I notice you using _sbx as sandbox folder... did I publish or suggest somewhere that I do so? If you found two overlapping hierarchies there, I should go and fix that to not mislead... The sandbox folder structure (on my system, crated a while ago, 2 years?) is as follows:
To make .../_sbx folder the Espruino IDE sandbox, just enter .../_sbx in the SETTINGS - PROJECT. No change of SETTINGS - COMMUNICATION. The modules are found in precedence as described in espruino.com/Writing+Modules. IDE knows into which folder it saved the last time... I do not know about the reinstall/update, what this does... I would not expect that it takes into account where and what it saved the last time and use it for sandbox default... which would create that mess.... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-09 by Robin That's the same as mine with the addition of a duplicate set under _sbx\projects that must have been added when I did the last update/install roughly six months ago. Haven't figured out how to map to the modules folder.
Just did the installations (then) using the defaults. From #9 above - there isn't any wrapper code that defines that snippet as a module.
Thanks for responding while jointly online, that has helped. Only have fifteen minutes before I've got to go. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-09 by @allObjects You do not map to the modules folder... if you you save code in the IDE, you have to pick the folder... and therefore, when you when you save modules, you have to pick another folder on save pop-op/dialog. But just watch out: IDE remembers the folder, and your next save goes into the same folder if you do not change / explicitly select it. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-09 by Robin Sun 2018.09.09 later same day . . . Back at it, several times, but with no luck. Console reveals that it doesn't appear to even be looking for the local folder. allObjects see new post:
http://forum.espruino.com/conversations/325378/ I remembered, but was unable to locate another individual post (around three-four months ago) that was attempting to use the projects folder, but it was suggested to place module code between While searching for that post, I did uncover this, where it seems an attempt was made to resolve this, but never completed. Maybe we are at that point again to revisit again? http://forum.espruino.com/comments/14110348/ WinXP: how can I require() some locally held modules? I'm at ver 0.68.6 from install ~six months ago. What version of native IDE are you using, and on a Windows PC allObjects? was able to create repository link to test
require("https://github.com/sleuthware/EspruinoDocs/blob/master/modules/testLED.js").create();
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-12 by @gfwilliams Looks like the other post you created on the IDE/modules issue is kind-of sorted now. In http://forum.espruino.com/conversations/279130/#14110348 you posted there's a link to an issue I filed, which is resolved: espruino/EspruinoWebIDE#186 I raised the timeout from 50ms to 250ms - but it seems that people just keep finding ever-slower computers to run the IDE on :) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-09-12 by @allObjects
Back to the future, but future is not even close to today. Dealing with timeouts / polling in absence of events, it is difficult to be speedy and efficient and still reliable... (yes, there be an timeout event - for example, http timeout/404 - but the timeout is so far out in the boonies, that one tries to take chances... and become unreliable). It obviously is what it is. These timeouts may one day become part of the settings so the unearthend, flintstones owned computers still can be used for IDE... ;-) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2018-09-08 by Robin
Sat 2018.09.08
I am attempting to test a module function between
var exports={};
statements.The IDE shows correct syntax check in right-hand editor pane.
On immediate send to device, Uncaught ReferenceError: "testUR" is not defined
despite the fact I just created the function definition. Observing the 'this' contents confirms this
Ideas anyone?
Beta Was this translation helpful? Give feedback.
All reactions