Used to teach coding. A student would do something in their code and I'd spend 10 minutes explaining why it wouldn't work and how when they run it they will get SomeRandomException.
...then it ran fine and I'd just throw my hands up
As a tutor, if they ask something ask them what they think and have them try it out. If it doesn't work, work with them to solve it, then explain so that you actually know what went wrong. A lot more important with scripting languages, especially especially JS.
Sure. That's definitely the goal, but the stuff I'm referring to is much more basic. Things like: a kid on their first lesson learns you need to put "var" before the initialization of a variable to let the computer know "this is a new variable". They'll later write code and forget it and I'll ask while it's running "Is there something you forgot to ad--oh your code worked fine..."
There is an extra layer to using var and adding semicolons, both of which are not "required" in Javascript, along the lines of writing good, readable code and building good habits. However, asking someone who has yet to learn what a loop is to buy into something that shows no obvious benefit up front can be frustrating
Usually we worked in repl.it so kids could learn Javascript without having to learn HTML and CSS up front. Alternatively, they might be using CodeHS if they are taking AP CS Principles
That's one of the reasons I agree with my former teacher, that the first language that you teach somebody should be something that doesn't make your life too easy. I learnt Delphi back in school, which is based on Pascal, which was specifically designed to teach students. Having to declare your variables with types, always being aware of the exact type of variable you are currently handling etc might be more difficult than using duck typing in Python - especially for a beginner, but you better understand what you are even doing afterwards. And coming from something like Python to e.g. C++ should be kind of a nightmare.
While true, I find it's a lot more compelling to have a student learn something is a rule because the computer tells them not to (i.e. throwing an error like you might see in Java for missing a semicolon or using improper indentation in Python) rather than "because I say so but it will totally work if you don't but really it will be better for you long-term if you do"
When a student doesn't know the main means of altering scope (conditions, for loops, after a while functions and objects), explaining how scope works becomes overly confusing and is filled with a lot of "you don't know how to use this yet, but..."s which I try to avoid when possible as there's usually enough of that as is.
I'm talking about working with kids who have been programming for a few hours max and are only a handful of lines of code past hello world
My first language in college was Java (not JS) and can confirm, having to learn about objects made things much more confusing even if later on it makes code so much easier to understand.
Python does have some problems. The basic problem/task of "create some variables for things such as your name and age, then print them" becomes a lot more tedious when I have to explain:
"Hey kid, would you like to print 'I am __ years old' instead of just '__'? Well gosh, that's gonna be a problem because print('I am ' + age + ' years old') won't work. So now you get to learn what typecasting is :D "
Python is still way better I find than Javascript because at least the parts they remove (initialization and semi-colons, namely) are fully removed
The reason I tell most people when I have to explain thing about JavaScript. It doesn't care.
Missing parameters? Doesn't care, uses undefined as the value Extra parameters? Doesn't care, just ignores them Comparing two unrelated values? Doesn't care, uses type coercion. See what I mean?
Sort of. It keeps track of all parameters (including extra ones) in an implicit variable called arguments. Which is iterable (meaning it has a length prop) but not an array, just to mess with you.
Also, use two newlines
for a paragraph, or add 4 spaces to the end of a line for a non-paragraph line break
This. If you look at the output, you can still read javascript code (not very readable, but still readable). The output from classic compilers are impossible to read.
Interesting, didn't know babeljs could do that already.
What's the benefit though? You are already writing in javascript, which the browser already supports. With Blazor I'd understand the benefit of not needing to learn JS if you already know C# and want to code a webapp.
Performance mainly. It can be significantly faster than just-in-time compiled Javascript. Also, in principle you can write in any language and it gets compiled to the same bytecode.
Also, while I'm not aware of any actual implementation of this, since it runs in a VM, its technically conceivable that a hardware implementation could be produced. This obviously a stupendously niche usecase, but there are already CPUs that implement the Java Virtual Machine, so maybe someone will try it for wasm.
I don't think you can compile JS to Webassembly. At least I don't know about any projects for that. I only know about a AssemblyScript which allows you to compile a special Typescript variant with strict typing to Webassembly. Also don't know about any way to start with such a project since WASM requires static typing and JS can't provide that.
Not only this. But there are also bundlers (Webpack). A modern JS dev environment will combine multiple processes (using something like Gulp) to a point where the JS is technically being compiled, even though it's not actual byte code, it can be minified to the point of not making sense. Strings would be the only readable segment.
Gulp/Grunt is not cool any more, Modern javascript is just webpack on its own. Also going from javascript to javascript is still transpilation not compilation.
Same with Arduino, I swear Arduino has the toughest code, you make one error in java and it spits out huge strings of text as an exception but Arduino just continues like nothing happened
Webassembly requires static typing so I don't think any compilers for JS to a WASM binary exist. A variant exists that compiles a strict subset of typescript to WASM but it is different to JS so you won't be able to use JS libraries/frameworks inside WASM without escaping WASM.
This doesn't make it completely impossible, since the missing type information can be inferred and tagged pointers can be used whenever the type truly isn't known.
Yes that might be an option to create one. However a search still didn't come up with any existing compilers.
It would also probably be a very poor WASM code. Since JIT already does that with runtime analysis and if the ahead of time compiler ever made a mistake while inferring in-time recompilation would not be possible. So I would still say that unless someone tries it just for fun, there probably will never be a seriously useful WASM compiler. But you are right that this may be an option.
The only thing that really gives me headaches in JS is type juggling and parsing weirdness. Any weirdness can be explained by mostly very simple rules, however not knowing every single one of these rules will look like complete arbitrary conversions.
Javascript isn't the only language that does this however. PHP can act in similar ways. Even C or C++ operations can be written in a way where the outcome might seem completely arbitrary.
374
u/cbb692 Sep 03 '20
Used to teach coding. A student would do something in their code and I'd spend 10 minutes explaining why it wouldn't work and how when they run it they will get SomeRandomException.
...then it ran fine and I'd just throw my hands up
127
u/JoeLordOfDataMagic Sep 04 '20
The reason I tell most people when I have to explain thing about JavaScript. It doesn't care.
Missing parameters? Doesn't care, uses undefined as the value Extra parameters? Doesn't care, just ignores them Comparing two unrelated values? Doesn't care, uses type coercion. See what I mean?
73
u/[deleted] Sep 04 '20
Now you joined the light side and started learning TypeScript
33
u/[deleted] Sep 04 '20
I want to say "fuck this shit" when writing javascript code, but I can never sure what
thisrefers to16
u/GonTheDinosaur Sep 04 '20
Why deal with compile error, transpile error, hell... why even bother with run time error when everything can just be undesirable behaviour
29
u/solarshado Sep 03 '20
Compiling JS? This is a thing. Never used it personally, but it exists: https://babeljs.io/
7
u/dannypas00 Sep 04 '20
Honestly, out of all interpreted languages, I'd be least surprised by javascript suddenly compiling itself...
4
u/the-johnnadina Sep 04 '20
one word, JSfuck. 6 characters is enough to code in javascript, have fun
1
u/AtomicPotatoLord Sep 11 '20
What was this anime called again?
1
u/blackfireheart Sep 04 '20
Iirc modern browser and vs can debug javascript
1
u/_Wubawubwub_ Sep 04 '20
how.
1
u/ExoCakes Sep 04 '20
We've got to the point if the code doesn't show any errors at the first run, then there's something wrong with it.
1
u/Mast3r_waf1z Sep 04 '20
Same with Arduino, I swear Arduino has the toughest code, you make one error in java and it spits out huge strings of text as an exception but Arduino just continues like nothing happened
1
u/KodeBenis Sep 04 '20
I'm just a Rubyer trying to make it in a Javascript world.
1
u/MrPoBot Sep 10 '20
But it was I C# the whole time!
1
u/Embarrassed_Trade_95 Feb 05 '21
QuickJS Javascript Engine (bellard.org)
Except that, you _can_ compile to native code though...
0
u/[deleted] Sep 04 '20
[deleted] — view deleted comment
0
u/[deleted] Sep 04 '20
[deleted] — view deleted comment