|
There's also JavaScript Allongé if you want to annoy the poo poo out of all your coworkers by writing Lisp in Javascript.
|
# ? Jul 16, 2014 20:26 |
|
|
# ? Jun 5, 2024 10:51 |
|
Wheany posted:Do you have any programming experience in other languages? I never took a real programming course, but I have experience in php and javascript. I think what I'm missing is not having the basic fundamentals down of programming. I totally get that programming is the same across the board, and that I'd just have to get the new syntax down. I can read code and can get by, (I still get confused on arrays though.) but I'd like to be sharper.
|
# ? Jul 16, 2014 23:56 |
|
I've stumped myself here on some form validation. I have a few select dropdowns with a text input next to each, like so: code:
code:
JSfiddle here: http://jsfiddle.net/EzP7Q/
|
# ? Jul 17, 2014 18:49 |
|
Is this what you're looking for? I'm not pushing the value to the array, but capturing it in a variable (because I was lazy). http://jsfiddle.net/HM3ve/ You also had some plain old \/\/ What he said about your inputs and selects. kedo fucked around with this message at 19:24 on Jul 17, 2014 |
# ? Jul 17, 2014 18:56 |
|
Iterate instead over the .aidInput divs instead, and pick out their children to process. Also, get your <input>s out of your <select>s, there might be kids watching.
|
# ? Jul 17, 2014 18:59 |
|
Subjunctive posted:Iterate instead over the .aidInput divs instead, and pick out their children to process. That works, thanks! kedo posted:You also had some plain old closure errors in your fiddle, just FYI. I'd be grateful for elaboration on this please. I know closure is basically a snapshot of a function and it's environment when it was called saved into memory but that's about the extent of it. **edit - is it because i'm not capturing the initial $('.amount').each() in a function? Raskolnikov2089 fucked around with this message at 19:16 on Jul 17, 2014 |
# ? Jul 17, 2014 19:01 |
|
**duplicate my bad
|
# ? Jul 17, 2014 19:02 |
|
Raskolnikov2089 posted:That works, thanks! See comments in the code below. JavaScript code:
e: Closure wasn't the right word. Syntax error is more appropriate. kedo fucked around with this message at 19:23 on Jul 17, 2014 |
# ? Jul 17, 2014 19:21 |
|
kedo posted:Closure wasn't the right word. Syntax error is more appropriate. Thanks! It's okay, it led me into a fascinating read of for loops and closure. Sorry for the sloppy copy and paste. Raskolnikov2089 fucked around with this message at 23:27 on Jul 17, 2014 |
# ? Jul 17, 2014 19:31 |
|
stoops posted:I don't know much javascript or jquery. I mean, I can modify and get things to work, but i'm not comfortable writing it from scratch. http://javascriptissexy.com/how-to-learn-javascript-properly
|
# ? Jul 17, 2014 19:57 |
|
I'd like to have function be called passing in a unique value for i, but whatever listener I activate, when the function is called i always equals 4 - the amount of times that for loop runs.code:
edit: I figured it out! I think this is called a closure, not sure: code:
LP0 ON FIRE fucked around with this message at 16:18 on Jul 18, 2014 |
# ? Jul 18, 2014 15:41 |
|
Yeah, you got it, before you were capturing the variable i rather than its then-current value. Calling the function with the argument (which I might name something different for clarity) passes the current value, which is then captured in the inner closure.
|
# ? Jul 18, 2014 16:58 |
|
That is extremely strange and unclear to me not coming from a Javascript background. I see what's happening but it's baffling and not obvious at all.
|
# ? Jul 18, 2014 18:33 |
|
Javascript is for the win!
|
# ? Jul 18, 2014 18:50 |
|
As a general rule you should always be careful when creating functions in loops, for exactly this reason. In a programming language whose closures capture by reference (JavaScript, Python), this behaviour does make sense, and there is unfortunately no other behaviour which makes sense (nor any syntax which would allow you to capture i by value). You can also do: JavaScript code:
|
# ? Jul 18, 2014 19:23 |
|
qntm posted:.. In a programming language whose closures capture by reference (JavaScript, Python) .. So in a way, without the closure, it's almost like having the variables (i) defined outside the loop and the new functions always referencing those?
|
# ? Jul 18, 2014 19:31 |
|
LP0 ON FIRE posted:So in a way, without the closure, it's almost like having the variables (i) defined outside the loop and the new functions always referencing those? Internally, that's exactly what it does.
|
# ? Jul 18, 2014 19:41 |
|
Peanut and the Gang posted:Internally, that's exactly what it does. Freaky. I'll try to get used to thinking this way.
|
# ? Jul 18, 2014 19:52 |
|
That's why some people like declaring all their variables at the top of the function even though it makes the code look ugly.
|
# ? Jul 18, 2014 19:54 |
|
Peanut and the Gang posted:Javascript is for the win! This isn't particular to Javascript, it basically always happens with closures. C# has the issue too (http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx). Apparently they've fixed it for the foreach loops by making it so that each run of the loop has a separate value which can be closed over. That doesn't fix it for the standard for loop however. Incidentally, I believe the Javascript .forEach (not foreach) shouldn't have this problem.
|
# ? Jul 18, 2014 21:31 |
|
Peanut and the Gang posted:That's why some people like declaring all their variables at the top of the function even though it makes the code look ugly. But that's what the JS interpreter does anyway, right? It doesn't matter where it is in the function, it defines all the variables at the top so it's just good practice to get in that habit of defining them up there so that you don't run into a situation where you're expecting one thing and the interpreter does another.
|
# ? Jul 18, 2014 21:34 |
|
In ES6 you get let which is lexically scoped to the for's body block, so it behaves the way you want here. Not sure which browsers support it yet, other than Firefox.
|
# ? Jul 18, 2014 22:37 |
|
HappyHippo posted:This isn't particular to Javascript, it basically always happens with closures. C# has the issue too (http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx). Apparently they've fixed it for the foreach loops by making it so that each run of the loop has a separate value which can be closed over. That doesn't fix it for the standard for loop however. Yes, that's exactly what I just said.
|
# ? Jul 18, 2014 23:31 |
|
Here's more on variable hoisting in JS if you want to read a complete piece about it. http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
|
# ? Jul 19, 2014 17:22 |
|
hedgecore posted:Here's more on variable hoisting in JS if you want to read a complete piece about it. Thank you.
|
# ? Jul 19, 2014 18:43 |
|
Just a real general question from a JS noob. I am looking for a small script to embed into html that after a determined time turns the page a set colour, or just shows and image. Then after that is viewed for a set amount of time returns.... The application is for submitting web and graphic design work and avoiding unscrupulous people copying the work and not hiring me, or paying me or whatever. So is JS the best option for this, will it be easy to do with some googling about?
|
# ? Jul 21, 2014 18:29 |
|
thegasman2000 posted:Just a real general question from a JS noob. Your best bet is to get paid up front. Really there is no way you're going to protect graphics online. Your script can be disabled (just turn JS off in the browser) and the file can be lifted. Sure you can do some pretty basic stuff to try and protect your assets, but remember they can all be defeated by someone with just a little know-how.
|
# ? Jul 21, 2014 19:41 |
|
excidium posted:Your best bet is to get paid up front. Really there is no way you're going to protect graphics online. Your script can be disabled (just turn JS off in the browser) and the file can be lifted. Sure you can do some pretty basic stuff to try and protect your assets, but remember they can all be defeated by someone with just a little know-how. Yeah. Post low-quality thumbnails, send real assets via Dropbox link or whatever when you have the contract in place.
|
# ? Jul 21, 2014 22:29 |
|
Pfft you don't need functions to enclose variablesJavaScript code:
|
# ? Jul 22, 2014 03:44 |
|
of my language, you savage
|
# ? Jul 22, 2014 03:49 |
|
Strong Sauce posted:Pfft you don't need functions to enclose variables jscript bugs are the best
|
# ? Jul 22, 2014 04:03 |
|
Suspicious Dish posted:jscript bugs are the best I'll bite, what's the bug?
|
# ? Jul 22, 2014 04:03 |
|
The only reason try/catch creates a new scope for the variable is because JScript accidentally implemented it that way.
|
# ? Jul 22, 2014 11:59 |
|
Suspicious Dish posted:The only reason try/catch creates a new scope for the variable is because JScript accidentally implemented it that way. That's not my recollection, and I think I was the first person to implement JS exceptions. JScript had a bug where it created the variable in the enclosing function's scope, rather than one that matched the duration of the catch block, which I think is the opposite of what you're describing, but exceptions were always specified to create a new scope chain entry with the identifier bound. 15 years, though, so yeah I'd love a reference if you have one.
|
# ? Jul 22, 2014 12:56 |
|
Subjunctive posted:That's not my recollection, and I think I was the first person to implement JS exceptions. JScript had a bug where it created the variable in the enclosing function's scope, rather than one that matched the duration of the catch block, which I think is the opposite of what you're describing, but exceptions were always specified to create a new scope chain entry with the identifier bound.
|
# ? Jul 22, 2014 15:59 |
|
Misogynist posted:The people we get on these forums Eh. There are no doubt lots of people on SA who could have written the code, I just happened to be right place/right time more than a 20-year-old has any right to expect. I bet you elicited some serious from Mr Dish, though!
|
# ? Jul 22, 2014 16:08 |
|
Inspector on Safari is reporting that a multidimensional array at a certain index equals undefined and can not evaluate it even though it seems everything is working normally. The error is on this line:code:
|
# ? Jul 22, 2014 16:24 |
|
And?LP0 ON FIRE posted:Inspector on Safari is reporting that a multidimensional array at a certain index equals undefined and can not evaluate it even though it seems everything is working normally. The error is on this line: What is keyTimesAndImages[window.IDNum]'s value? There really isn't much here to go on...
|
# ? Jul 22, 2014 16:26 |
|
Subjunctive posted:And? Oh sorry. Inspector says it's undefined. It's supposed to be an array. Ahh, edit. It says ["default", "groceryStore.jpg"] undefined edit 2: Okay I think I'm going crazy or there's something weird going on. The first time I told you it said it was undefined that's what I thought I saw until I looked at the console again and pasted what I saw differently. I'm now trying to get the value even after reloading and it just says undefined. LP0 ON FIRE fucked around with this message at 16:38 on Jul 22, 2014 |
# ? Jul 22, 2014 16:31 |
|
|
# ? Jun 5, 2024 10:51 |
|
LP0 ON FIRE posted:Oh sorry. Inspector says it's undefined. It's supposed to be an array. In that case [w.IDNum][0][1] should be "groceryStore.jpg", if I'm understanding your post correctly, so I suspect you're not interpreting the error correctly, or else we're misunderstanding each other about what to print here.
|
# ? Jul 22, 2014 16:37 |