|
|
Beats me. I haven't touched Perl in a loooong time. You're on your own.
Jeremy Falcon
|
|
|
|
|
Now explain closures and you'll have everyone halfway there.
And of course the obligatory WAT video whenever Javascript weirdness is raised.
Wat[^]
cheers
Chris Maunder
|
|
|
|
|
Chris Maunder wrote: Now explain closures and you'll have everyone halfway there. You'll hear 40 million different definitions online about that. And most of them try to sound smart, but put simply it's a function within a function. People will try to tack on crap with scoping etc. to that definition when "explaining" it, but that's irrelevant to the core concept of it IMO.
Chris Maunder wrote: And of course the obligatory WAT video whenever Javascript weirdness is raised. Ok, that dude is hilarious.
I have no idea why the object stuff in JavaScript turned out the way it did, but I can at least explain the 'wat' + 1 thing. The plus sign is used for string concatenation and arithmetic. The minus sign isn't. So in the former example, the interpreter assumes you're trying to concatenate using the most compatible types between the two operands, which is usually a string. The minus sign only only used for arithmetic and so it's NaN.
Jeremy Falcon
|
|
|
|
|
|
Jeremy Falcon
|
|
|
|
|
Jeremy Falcon wrote: I have no idea why the object stuff in JavaScript turned out the way it did .
Because Mozilla were insane enough to give a dev just 2 weeks to write a language. It's amazing it works at all.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
You have a good point.
Jeremy Falcon
|
|
|
|
|
Javascript has become a powerful language. With this comes responsibility
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
Wisdom you speak. Wisdom you speak...
Jeremy Falcon
|
|
|
|
|
KarstenK wrote: avascript has become a powerful language.
And Hitler was a powerful dictator.
|
|
|
|
|
I am currently writing a road traffic simulator in javascript and both at work and in this project the one thing that makes javascript difficult is scope.
Basically the manner in which scope is implemented javascript makes it that much more difficult than it needs to be.
That said I have found that programming in javascritpt has made me a better developer because there are so many ways you can do things badly in javascript.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
modified 4-Dec-17 13:21pm.
|
|
|
|
|
I know what you mean. I do find the more I use JavaScript though the more I like it. It's just where the web is at, so getting can only help ya know.
Jeremy Falcon
|
|
|
|
|
Personally I find any issues that arise from scope can be resolved by the immutability of objects. I have no idea if this is good practice, but passing object references around or just having a global or high-up object with important stuff is how I like to do it.
|
|
|
|
|
I agree with you it is not such a bad language as people try to picture it.
|
|
|
|
|
Thanks man. I totally agree. It's just different is all.
Jeremy Falcon
|
|
|
|
|
And it is still safer than C or C++ - because these two have undefined behavior.
|
|
|
|
|
Yup. Don't get me wrong, C is still a favorite language of mine. Always will be, but it's harder to take down the entire OS in JavaScript.
Jeremy Falcon
|
|
|
|
|
Yeah I see just the feature of undefined behavior (in case you are familiar with this term) is a bad feature of those languages IMHO.
|
|
|
|
|
I think you missed the point that it's fashionable to rip on JS because you "should" be using TypeScript.
#hipstercode
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
Probably, but can't go and be a rebel and all and start using that now can we?
Jeremy Falcon
|
|
|
|
|
Tried the code in a new HTML page using Visual Studio 2017 and Google Chrome. The first alert showed "Apple Apple" and the second "Apple undefined". "Orange" did not show up.
TOMZ_KV
|
|
|
|
|
Double check how the project is set up then. Here's a fiddle of it...
JSFiddle[^]
Jeremy Falcon
|
|
|
|
|
I tried JSFiddle and it worked. However, when I copied the exact code to a new HTML page, it behaved the same way as it was without "Orange". Here is the complete HTML code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
<pre>
var fruit = 'Orange';
window.fruit = 'Apple';
function explainThis() {
alert(fruit + ' ' + this.fruit);
}
explainThis();
var stuff = new explainThis();
</script>
TOMZ_KV
|
|
|
|
|
Gotcha. It's because stuff ran on JSFiddle is a mock root and not the real root. When using the real root, as in your case, window is the default object, so in the real global scope the first two lines are pretty much the same since window is the default object and so the second assignment overwrites the first. In my original code, it was a fake root so to speak. I probably should've tested it first, but hey where's the fun in that.
To illustrate...
<!DOCTYPE html>
<html lang="en">
<head>
<script>
var fruitRoot = 'Banana';
window.fruitRoot = 'Pineapple';
function explainThisRoot() {
alert(fruitRoot + ' ' + this.fruitRoot);
}
explainThisRoot();
var stuffRoot = new explainThisRoot();
(function () {
var fruit = 'Orange';
window.fruit = 'Apple';
function explainThis() {
alert(fruit + ' ' + this.fruit);
}
explainThis();
var stuff = new explainThis();
})();
</script>
</head>
<body>
</body>
</html>
Jeremy Falcon
|
|
|
|