Correct syntax to reference a class function inside a class as a module #1217
Replies: 19 comments
-
Posted at 2018-10-08 by @MaBecker Try this: when using one file than
and there you go :-)
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-08 by @allObjects I see kind of a mixup of class vs instance method and the linking to module. First module has no direct connection to class. Only what is returned in a module can be interpreted as a plain function or constructor function, and latter can be understood as class method. In a Class based object-oriented language - which JavaScript is NOT (it's prototype based but can be 'emulated') - the class is (best) an actual (first class) object - and 'understands' only class methods, of which at least one is usually at constructer, either called a method - in In a constructor, If you need to refer to a class method in an class or instance method, you just name the class. Since in JavaScript the constructor (function) is practically the only class method (except you attach function attributes to the constructor function), you repeat the same class or another class - latter pattern is common where as former is rarely found (except in chained / linked / tree-d type of constructs). An example of the rarer kind would be the create and return of a directory that needs a creation of a multi-level path with the option of auto-create when missing... The example has the oddity of wasting the just constructed instance when already existing. In that case it would explicitly say Anyway, I hope this clears up some of the muddied terminology... which is difficult to respond to. It's like reading a riddle and then with the help of some 6th sense to assume what the comment or question is all about. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-08 by Robin Mon 2018.10.08 Attachment from #1 codeTestConstructorExports.js demonstrates working monolithic file: Thank you @mabe for your effort, but only works as a monolithic file. When deployed, still has the same effect, which allObjects pointed out. > 'I see kind of a mixup of class vs instance method and the linking to module.' Yes, agreed. I didn't think the casing of the class definition made a difference, and we are correct. It doesn't. See attached files.
As the instance is aware of it's properties, such as
May I ask how you determined this was a ES6 flavor? When I checked the 'features' link #1 above, I see 1v96 I made every effort to avoid any feature that may have played a role with the current version of the parser/compiler.
@allObjects, totally agree and is also a challenge to make sure the correct wording is used to describe along with the simplest of snippets to get that point across. Attachments: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by @allObjects @robin, I (still) try to understand what the context is when you use the term 'deployed'. In Espruino that term is not used. But from reading through your post, I looks to me that with 'deployed' you want to say that it is a Is that how you would define your term 'deployed (as a module)' ? Within Espruino the term 'inline testing of module code is a known term and way to develop a module together with the application code in the same file (in the Espruino IDE. Once done with the development, the module part is put into modules folder (or where ever you can pull it with So far the inline testing - as suggested in https://www.espruino.com/Writing+Modules for https://www.espruino.com/Modules- is something very brittle, because it uses the same name space... and that usually creates undesired side effects, constraints application in name choices, and worst, can lead to interference (breaking side effects). I started to use (also) a different approach to get the core issue - namespace problem - out of the way by loading the module cache directly, as described in the conversation about Module development - and shows explicitly in post #3. Inpost #3 you can see the module cache primed with the module source code and the module then retrieved by In post #8, this technique uses a newer feature of Espruino which allows to store modules as functions in the modules cache. Initially, the cache loading could only happen by providing source code, which is a test string. At a later time, @gordon introduced the storing as a function, which makes it equally simple to have the modules demarcation within the application when doing 'inline development and testing'. What ever you do in the module source - text string - post #3, lines 5 thru 33 - OR function body - post #8, lines 5 thru 42 in final code - is not visible to the application code what so ever. Furthermore, it seems to me that you see a class is different whether the class is defined inline or in module. There is no difference, because module is not tackling the structuring of software in classes, tackles the issue of separating source code into files, even though latter can be used to achieve former in order to enable reuse of classes without having to copy-paste source code... ;-) I suggest to take a deep look into Module development. I think it could help answering some of your questions and to develop your concepts even further. Btw,
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by Robin Mon 2018.10.08
Yes. Wanted to definitely separate the test file, module included, from the remotely fetched from GitHub module, concept. As GitHub is a remote server, the module is deployed there.
Nope. As I started out in #1 above, the class functions fine when in it's test container, but fails when used as a separate code and module. Deployed if you will. ;-) It is also why I supplied individual files with leading 'code' and 'module' so that whomever used them would just know where to deploy them. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by @allObjects EDIT: just noticed that while I was editing/adding to the post, you started to respond and we were working on the same conversation at the very same time... and that showed for me in weird behavior of the forum: it would extremely slow down the typing... it took seconds until a character would show... so I deleted all in this post #7 because it was a repetition of 6# plus a few edits and additional lines. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by @allObjects I pointed out the issue with inline testing when in same namespace... and that's what is failing for you. The context is different when the module is delivered from when you run the module source code inline in the application... because there it NOT a module. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by Robin Mon 2018.10.08
I thought some would read too much into what the code's intention was. The dilemma. Use all day to take a ginormous file and whack at it to make it as concise as possible to make a feeble attempt to get to a simplified snippet to use as the source, or use a concise solution and make a few code line changes to get the point across. @mabe had a nice easy to use proven solution that I chose instead. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by Robin
Yeah. Same here. Thanks for correctly indicating the edit in #7 as it did take me back when the content I was attempting to respond to, [ . . . . *vanished* |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by Robin Mon 2018.10.08 07:55pm CST Yikes, just heard on the national news that the latest Windows10 automatic update wipes out all the files in the 'Documents' folder!! . . . and they have no clue why!! 'Looking into it' they say. Good going Micro |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by Robin
Yes. I understand that. I'll repeat again. There is nothing wrong with the test file with the class (as a module) inline. The issue is when the module is used independently, called from a code file when using 'require()' From #1 above: "an error occurs when testing as a deployed module:" I also supplied that solution in 'codeTestConstructorExports.js' as proof. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by @allObjects (forum is messing with us again... so I try again:) ...but with an a logical error, as I point out in #8. Because the original code - writing modules using new feature class - interestingly *** also from @mabe *** - does not start the stopwatch on creation... just as I suggested. There is nothing wrong to add the auto, because you do not want to have the extra code of starting the stopwatch. That's why you / @mabe allow the "auto" to be passed. Makes absolute sense to me. To make the stopwatch robust though, it should object to start a started stopwatch with Illegal State Exception, which means that at the state the object is right now, a start is not possible... An additional property can indicate that the stopwatch is started or stopped. Combination of particular values of startTime and endTime could do that too: both are 0 or both are not 0 can do the test. Therefore:
See try...catch and throw in MDN...:
Regarding your proof: works as designed / as by your setup. There is nothing that needs to be proofed. It is how the system works and nothing is found at fault with it. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by Robin Tue 2018.10.08
No. No it is not what is failing me. Inline testing shows the code that will be in the module is syntax checking and run-time error free.
I will agree with the inline file, but the issue is with the two file version as I have explained and why I started this thread. The class properties and the class methods are recognized in the inline version. The class properties are recognized but the class methods are not in the two file version. I am seeking a solution to the last (previous) sentence only, not assistance in logic as outlined below. Please re-read #1 I explained in #9 the reason to make a simple modification to an existing working example. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by @gfwilliams Just coming to this afresh looking at just the first post (sorry if everyone else has already mentioned this), if you change:
to
It works great. Also, if you take your all-in-one file that works and change What you're doing is running the |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by @allObjects IMNSHO, using object oriented (oo) terms is not a matter of code snippets... it's a matter of communicating apples and oranges... less talking about comparing them. A good introductory start is http://www.academia.edu/download/32169601/Object-Oriented_Software_Construction.pdf - explains the concepts and the various language bindings. 1400+ page is a very looooong start. So let's get a shorter one - 2 clips:
To bad that it is Python, but replacing And there is MDN with https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects - at times confusing, because it shows all the options of oo pre-ES6 and ES6+ (and the ES6 modules aren't there yet... but the node.js). Now we do some changes to any code and discuss it... The shortest way though is sneak peek into the - or one of the best practice - solutions, like peeking at the solution for the Sudoku challenges in an airline magazine. Here the solutions, clear and straight, with all bells and whistles, and naming ad casing as best practices suggest (single saves and run... bulls-eye):
Output attached as screenshot. Notice the time sequence... StopWatch instance 3 gets bothered before instance 1 and 2 stop and show the duration. Instead of getting the class (line 3) and re-use it with all the Attachments: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by @MaBecker Wow great stuff @allObjects- thanks for sharing. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-09 by Robin Tue 2018.10.09 Ref #15 Short and precise along with the paragraph explanation. That is what I was after. Thank you @gordon. Placing the '()' and the argument correctly gave me fits, even with the IIFE construct present in the original source. I'm researching 'bare functions' as I've not heard that term before. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-10 by @allObjects 'bare function' is when it is not a method function of an object - that means it does not need or use a That this Think of two objects that have both a width and a length in rectangular form that defines their foot print, in addition to other things, that can be totally different - for example shipping container (8 feet wide, 8.5 feet tall and of different length (or flooring tiles) and you need to know the total surface - footprint - these objets cover (can cover) when not stacked. Btw, while reading this, paste the code pieces into the console... and you will see the results right away.
Now you can define a function that, when applied to any of these objects delivers you the footprint in square feet:
Question: how in the world does this footPrint know what
When applying this function to an object that it can handle or can be handled by it, we get what we want:
And when we want the total we do this:
and get 480. Now, we make it a bit more elaborated: We want to know what the volume is these container objects can store: how many cubic feet of what ever they can store. It is easy to understand that the longer container have a stronger structure - beams, walls, etc. and may therefore have less storage volume. Therefore we create a
Fort the total volume we use:
and get 2116.9999999999995 Now, you say, why all this mess, because we could define a Container class and have a constructor that takes values, and then instance methods for Exactly, this is what ES6 gives us. Deep under the hood though, exactly what we so cumbersome experienced, happens, even ES6 does under the hood the In this context belongs also the This allows us to invoke a bare function, but because it was bound before to an object, it is as if we invoke it as a method on the bound object. This is helpful for all cases where we can only pass a (callback) function, such as in setTimeout, setInterval, setWatch, and in many modules where things happen asynchronously or time deferred and we do not want to block the processor.
Executed we get delayed 160 as expected. What Sane is for the an ES6 created What we then say (in pre ES6) is:
The first part of the right side in first line The ES6+ version I leave up to you to do... ;-) piece of cake... and I can have the cake and eat it. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-10-10 by @allObjects Regarding the Think about of these as the execution or invocation method of the function object. In JS, all things are object... sounds like my screen name... does it? ...and sorry when it feels 'me being obsessed with objects'... I'am, kind-a, but it gets and keeps my daily job robustly going. Therefore, functions are objects as well. To 'get them going', you just throw
which is nothing else then:
or even more cumbersome:
Entering
just returns the function as object
fires it to say: haha Interesting - but expected from how JS works - is when requiring module that returns a constructor (class), that the require has to be 'precedenced' / precedence enforced with parentheses, and only after that requires completes, the new with or without parameters in parentheses can be thrown/applied at it:
Otherwise, the new goes against the require, and require is not a constructor with the module name as a parameter / will fail as a constructor... This fact shows the weird or mixed design of the There is more to - this - OO and Lambda (Calculus) constructs... Lambda Calculus, - something 'discovered'/formulated in the 30'. ...and about that may be at another occasion... - |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2018-10-08 by Robin
Mon 2018.10.08
Thank you @mabe for the working example:
http://forum.espruino.com/conversations/317394/#comment14111852
Q1: While
this.start()
does work when testing a module as a monolithic file:an error occurs when testing as a deployed module:
What is the correct syntax to reference an internal class method from inside that class?
Indicates Class Definition 1v96 Official using 1v99
Q2: An error occurs when using a multi-parameter constructor when a default constructor is present
Is the preferred method to use just the multi-parameter version and have implied optional arguments and code the single constructor for those options?
e.g.
if( typeof option == "undefined" ) {
if( option != "undefined" ) {
Attachments:
Beta Was this translation helpful? Give feedback.
All reactions