MoI discussion forum
MoI discussion forum

Full Version: Question about Java script

From: fcwilt
8 Aug   [#1]
Hi,

I noticed this is a file ScriptsFull.menu.htm:

for (var d in dirList)
{
var files = moi.filesystem.getFiles(dirList[d], '*.js' );
for ( var i = 0; i < files.length; ++i )
{
// some more stuff was here
}
}


The files are accessed using a for loop and an index variable.

Is this because the file list does not support the form as used for the directory list - for (var d in dirlist)?

Thanks.

Frederick
From: Michael Gibson
8 Aug   [#2] In reply to [#1]
Hi Frederick,

re:
> Is this because the file list does not support the form as used for the
> directory list - for (var d in dirlist)?

Yes, here the dirlist variable is a native JavaScript Array that was created by the JavaScript runtime.

The files variable is a MoiList object created by Moi,

Until recently in v5 objects that were created by Moi did not support the for ( propname in object ) syntax.

Since v5 Mar-29-2023, the for ( propname in object ) syntax is now supported on objects created by MoI but it doesn't have the special behavior for JavaScript arrays.

It's generally considered a best practice to use a regular for () loop with an index variable to loop through the contents of an array and not for ... in .

Some info here:
https://stackoverflow.com/questions/5263847/javascript-loops-for-in-vs-for
https://www.geeksforgeeks.org/javascript/why-using-for-in-for-array-iteration-a-bad-idea-in-javascript/

- Michael
From: fcwilt
8 Aug   [#3] In reply to [#2]
Hi Michael,

Interesting.

So it doesn't work anything like the 'for in" I use in Delphi. There the programmer gets to/has to code the class enumerator and thus "for in" can do exactly what he wants it to.

Over the years I am often bemused at some of the things that Java and JavaScript do.

Thanks for the references as well.

Frederick
From: wayne hill (WAYNEHILL5202)
11 Aug   [#4]
Use JavaScript version ES5 if generating code for MoI3d from AI.

Wayne
From: fcwilt
11 Aug   [#5] In reply to [#4]
Hi Wayne.

So AI can generate code?

How does it know what to do?

Thanks.

Frederick
From: Frenchy Pilou (PILOU)
11 Aug   [#6] In reply to [#5]
Of course Ai can generate code about anything! :)
But you must manage it with some Q&R! :)
Send an image or/ and text who explain that you want in any domains!
I have absolutely no knowledge ot the five or more languages used! (Htlml, Css, render, JavaScript etc...

Here a little headache for play with it! :) Just fill the screen with same form who can be Rotated or Mirrored! ;)
Free Claude was used ... it can of course gives the answer but better oneself to seach by pleasure!
https://moiscript.weebly.com/jeux.html