|
dancavallaro posted:What is an example of a use case where it makes sense to use == over ===? The only time I ever had trouble with not being able to use === is if I had to check for "undefined" for some reason. For some reason it always failed (not sure if it still does) One of my former coworkers pissed me off by constantly doing this: code:
|
# ¿ Sep 22, 2010 18:30 |
|
|
# ¿ Apr 29, 2024 12:05 |
|
Golbez posted:My question was more along the lines of, why would geeves' coworker even need to do a 'typeof' in this case? The only possible type that can ever == "something" is a string. It's not like == "1", where a string or int could work. That's the point. My coworker didn't have to check for typeof because it was already handled in a method that would always return a String or an Integer or Boolean, etc. Even though JavaScript doesn't support real strong typed members, it's just something I've become picky about from my experience in Java and brought into my JS coding and it's also greatly reduced the type I've had to spend debugging.
|
# ¿ Sep 22, 2010 21:08 |
|
I don't know if this is a jQuery thing or not, but has anyone every successfully made a variable inside an anonymous function readable outside of it? We did this at my old job so we would avoid collisions amongst us writing lots of JS. But I didn't come up with how it was done and I've been unsuccessful in trying to recreate it from memory. It was something maniacal involving the window object. code:
Any ideas?
|
# ¿ Oct 9, 2010 03:00 |
|
Xenogenesis posted:Just assign that variable to some arbitrary property on "window". The window object is also the top level scope, so any properties you define on it are global variables. Simple example: That was it, thanks. It has been a couple of months since I've seen the code and I didn't write the part that started with ($, window, function) ... so I don't know why that was in there and I had too many other things to work on that I never got around to doing a code review before I left my old job.
|
# ¿ Oct 9, 2010 22:10 |
|
Lumpy posted:Javascript will add semi-colons for you. And that's a Bad Thing. On the other hand, I'm not sure why anyone would define their object in the actual return value instead of prior to it.
|
# ¿ Jan 5, 2011 05:07 |
|
Lumpy posted:I do it constantly, and it's one of the "big three" patterns in JavaScript, the Module Pattern. I admit I don't like the way it looks. Perhaps because I would never do that in Java. Especially if the returned object was more complex than just 3 lines. Is it interpreted differently than code:
|
# ¿ Jan 5, 2011 06:15 |
|
All Hat posted:JSLint complains about updateLinks() being called before it's declared. poo poo works, but I wonder if this is a sign of bad coding practices. I've never gotten this one, but if it's there this is one of those nit-picky things with which I don't agree in JSLint. Functions and declared variables are always hoisted to the top of their scope by the interpreter so they're ready to be used by everything in its scope. http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting
|
# ¿ Mar 22, 2011 17:03 |
|
Lumpy posted:Except Hoisting does not include the definition of the function (or assignment if it's an expression) if it's declared as a variable: Good catch - I thought that article covered it, it must have been another one or maybe in The Good Parts?.
|
# ¿ Mar 22, 2011 18:05 |
|
epswing posted:Use parseInt! It's better to check for field length and/or use a regex with zip codes because using parseInt could lead to unintended consequenses for codes that start with zero. http://www.w3schools.com/jsref/jsref_parseInt.asp so instead, something like: code:
geeves fucked around with this message at 20:32 on Mar 23, 2011 |
# ¿ Mar 23, 2011 19:29 |
|
Master_Odin posted:How would I submit a form inside an iframe? The form is set-up correctly that outside the iframe, it submits fine and what have you. However, when trying to do: Try something like this with jQuery: code:
http://forum.jquery.com/topic/jquery-how-to-access-iframe-window
|
# ¿ Apr 13, 2011 04:28 |
|
Lumpy posted:You should put your own semicolons in, but not for any real concern about performance. Putting semicolons in explicitly makes it clear "what you meant" to others (and to yourself in 3 months when you revisit your code) and also avoids some functional "gotchas" where letting the interpreter put them in for you might not give you what you expect / want. Non-use of semi-colons or not using braces is only of my coding pet peeves with c-style languages. I've tried to contribute to some opensource (mostly jQuery plugins that I use) when I can, especially ones with promise that fall victim to poor coding standards. Only to have the changes rejected and comments deleted when I raise concerns.
|
# ¿ Jul 6, 2012 21:34 |
|
Mindisgone posted:I can just call that in the middle of the code? What sort of info will I get? You can drop anything (strings, objects, arrays, html, etc) into console.log(Obj) and it will display in Firebug or your Javascript Console.
|
# ¿ Jul 9, 2012 19:59 |
|
Blinkz0rz posted:I'm getting a bit deeper into JavaScript and I was wondering if anyone had any suggestions for reading about anonymous functions, object literal usage, and general patterns for advanced programming? Here's an online book about Design Patters in JavaScript that I've used as reference http://addyosmani.com/resources/essentialjsdesignpatterns/book/
|
# ¿ Sep 6, 2012 15:08 |
|
Dr. Poz posted:I just ran into the fact that when instantiating a Date in JavaScript, the constructor parameter for Month is zero-based, meaning that a 1 means February. Is there a clean way to globally override this without making developers remember to use a derived type? I'm using TypeScript for this particular project if that makes a difference. I've done this in the past as necessary. Drop this into a global JS file. http://jsfiddle.net/VrU2a/ code:
|
# ¿ Jul 29, 2014 20:19 |
|
I have a stupid angular question. I'm trying to get program ID from ng-init, but it's undefined because reasons. I can dump the scope object and the value is defined in the object, with the correct value, however trying to access it doesn't work.code:
code:
|
# ¿ Jul 17, 2015 13:11 |
|
Jabor posted:Be careful with logging $scope and interpreting the result - the behaviour of the debugger can be more confusing than enlightening. Specifically, the log will show you the value of the $scope object when you clicked the arrow to expand it, not what the value was when the log statement ran. I'm just irked with the inconsistency more. I've run into things like this with JS many times in the past. I just went through everything and changed methods to make sure everything runs after the page loads. Everything is fine and back to normal.
|
# ¿ Jul 19, 2015 17:20 |
|
Knifegrab posted:I just can't wrap my head around promises. Does anyone have a fool proof tutorial/explanation? I've been reading through these books - https://github.com/getify/You-Dont-Know-JS Here's the book & chapter that discusses Promises: https://github.com/getify/You-Dont-Know-JS/blob/master/async%20&%20performance/ch3.md Edit: Hopefully that helps. I'm probably in the same boat as you. I won't see more benefits until I see more real world examples. geeves fucked around with this message at 20:40 on Jul 23, 2015 |
# ¿ Jul 23, 2015 20:37 |
|
Tip posted:Working with the DOM is super slow and you're reading and writing to the DOM for every single link. My suggestion would be to grab all the data at once, manipulate it in JavaScript, and then make one update to the DOM with the new values. Before the advent of jQuery when adding through the DOM was necessary and innerHTML not feasible because of scoping issues, this was definitely the recommended way to build a large amount of HTML (and really just in general)
|
# ¿ Sep 4, 2015 19:04 |
|
German Joey posted:Yeah, I just saw that too, and its finally the tipping point for me where I just gave up on node.js completely and go back to Django, or maybe Flask or Mojolicious, I haven't 100% decided yet. But absolutely that blogpost was the moment that I just said "gently caress it, I'm out, I can't deal with this node.js poo poo anymore." I'd rather spend a couple weeks rewriting my entire project then waste one more day fighting with a broken library. Maybe Sequelize is good now (I remember looking at that one specifically a few years ago and it being inadequate, although that was probably an early version because this looks OK), but the trend is pointing to probably not. I'm so sick of every loving javascript library being terrible!!! ARRGHH! Time after time after time, every new thing I look at has some flashy splash page but lovely, outdated, and inconsistent documentation. Every framework has ten trillion pointless preprocessors being stapled to it that otherwise reinvent the same drat wheel. If you need layers upon layers upon layers of build tools for your server-side javascript, then what the gently caress is the point of using javascript on the server side to begin with? Isn't the whole point that both client/server are in the same language, a language that everyone in the entire drat world happens to know and have access to - a language that any ten year old can start playing around with by pressing ctrl-shift-j in their browser? That's why I got into node.js to begin with! Next time I'm in an interview, if I'm asked about node.js this will be my answer
|
# ¿ Sep 8, 2015 21:28 |
|
Noam Chomsky posted:Which is? Free-form content. One of the best examples (regardless of if you think it's good) is Adobe CQ. It runs on a Java Content Repository JSR-283 is the current implementation off the top of my head (I may be wrong). While the content is structured by type (String, Integer, Date...), it's not bound by a DB structure and is stored in a nodal way so it is very conducive (and CQ has this built in) to simply exporting objects straight to JSON or XML or even HTML.
|
# ¿ Sep 19, 2015 01:47 |
|
Subjunctive posted:let doesn't really improve things for garbage collection very much (a bit in cases where you create a lot of garbage in one loop without reusing variables or something?), but binding-per-execution is useful for avoiding traps like the common: I use this as one of my interview questions and ask them, "What's wrong, if anything at all?" 2 out of maybe 14 or 15 candidates this past summer (for a senior-level / managerial position) got the answer right. One asked why I didn't just use jQuery
|
# ¿ Sep 23, 2015 17:08 |
|
Tao Jones posted:Angular question: Is it something you can track in $rootScope? It may or may not be the perfect place for it, but it's a start. In the old days of JS i would track this via cookies or browser cookies/session depending on the need. It might not be pretty but it worked.
|
# ¿ Oct 7, 2015 02:07 |
|
Skandranon posted:Using $rootScope is almost always a bad idea. I hate the idea of rootScope, like I said, it's not pretty. But it can be useful in the worst situations. It's the first thing that came to mind, but I should have suggested a factory. It's more natural to Java devs like me. If you're goal is you still have to track state somehow, root or or a factory object would be a good choice. I'm letting the fact that I hate the implementation of angular that a couple of devs did for my company's app. I wasn't involved in the decision (or dev) and I really wish I was. Because I would have veto'd it (or at least the way that they did it). I'm not against angular at all. I like it actually. But the web devs that we had wrapped our entire app AND javascript within controllers causing major pain and conflicts with new controllers, etc. that we've had to write. That and we still have a heavy reliance on jQuery which just adds to the mess. Those two devs are gone now (they were good, just not wise in the way they implemented angular) and I've moved all the rootscope methods to a factory where they should have been. But we have 1 module they defined and everything conficts if I want to use more than one controller on a page. I want to hire them back so I can fire them because gently caress they put us in a bad position that I can't easily fix / refactor.
|
# ¿ Oct 7, 2015 03:20 |
|
Skandranon posted:For the last month I've been working on refactoring a similarly poorly implemented Angular app. They used controllers like services, spawning up 10 of the same controller when they thought they were accessing the same one. However, it all magically worked because they ALSO created a hast table based global variable service that everything got put in, so the controllers ended up accessing the same variables from there, though I suspect this was more a coincidence. I don't understand it yet because I'm still figuring things out.... but they made every page its own controller. so: <body data-ng-controller="${actionName}"> (we're based in struts) wraps everything else. The controllers interfere with each other causing a major JS error (what I don't have a complete understanding of), yet. The embedded controller doesn't "fire" or "init" at all and I have no idea why. If I use directives,it works,, but it doesn't answer why the controllers don't. And perhaps that's one of the shortcomings of the 1.x branch that may be better addressed in 2.x
|
# ¿ Oct 7, 2015 03:42 |
|
Skandranon posted:I'd break everything down into smaller parts until the 'super controller' had no actual code left in it. This is really want I want to do. It's just not the easiest to sell in corporate environment because "everything works". My opinion (and agreed upon) is that the current implementation is completely wrong. Everything they did was in an init or return block that was just a clusterfuck.
|
# ¿ Oct 7, 2015 04:15 |
|
Bruegels Fuckbooks posted:My company used to use Lotus Notes, and then the announcement went out that we were going to use gmail. It was great - we could use it on our phones, just use it on any web browser. It worked pretty well for 4 years. That is loving insane. Was two-factor not an option with Gmail? For a company to be ISO-something-security compliant, you just need two factor and password changes every 3? months I believe. I've worked in places that required security clearances and even then there is nothing as insane as that. Edit: Sounds like your company got a bunch of FUD as a sales pitch to buy just another layer of "protection" which is just another places where employee information is held by yet another company.
|
# ¿ Nov 10, 2015 18:12 |
|
Lumpy posted:So I was looking at some code and came across a bunch of stuff like this: Let me guess, this is an NPM project...
|
# ¿ May 10, 2016 00:23 |
|
vardyparty posted:Semicolons or no semicolons? I had someone say this to me in an interview. Also about not using { } with if / else statements. He wasn't hired. Strong Sauce posted:Adding semicolons won't help someone in this case. Learning how ASI is implemented will. Yeah you're example is kind of tricky because to someone new to programming might not realize that an object is just an array of a map (key/value pair) so a comma would be used in that case (if there were more k/v pairs after). quote:The answer should be choose whatever works best for you. If you're writing code just for you - do whatever you want. I personally think it's lazy and in poor practice not to use them. I've always been strict with semi-colons; I'm from a Java background, so it's just more natural to me. In a team environment they should be enforced so there's less possible misinterpretation. At my company your code will not pass peer review. The time it can take to submit, reject, resubmit fixed code takes up more time than just using a semi-colon.
|
# ¿ May 26, 2016 00:23 |
|
Portland Sucks posted:I tried throwing What about something like: code:
|
# ¿ Aug 15, 2016 03:50 |
|
Portland Sucks posted:Sorry to be a pest, but I'm back with this same problem that remains unresolved and is rapidly become a pain in the rear end. Any other JS/Ajax gods around here that can lend a hand? My version worked. Try it yourself. You should get a few Alert dialogs then you'll be redirected to Paypal. http://plnkr.co/edit/Cpfsy5tn3gdhajTorcON
|
# ¿ Aug 19, 2016 13:08 |
|
Wheany posted:Is there an easy way of detecting when external images on SA fail to load? Not tested and probably could be a better solution, but something like: code:
|
# ¿ Sep 1, 2016 19:24 |
|
Blinkz0rz posted:Yeah, I should amend my comment to be: A company I worked for fired a couple of CMU JavaScript Bros for not adhering to our style guide. They had other issues too, but we were sick of denying their pull requests and having them break the build because it wouldn't pass JSLint, etc. They had other issues as well, mostly their attitude (and they were certain they were creating the new snapchat), but still it just carried over and dealing with them during code reviews, etc. became a nightmare because of it.
|
# ¿ Sep 19, 2016 22:49 |
|
Because you have multiple return points which, IMHO, is bad code. Add: return advGuessMyNumber(input - M... return advGuessMyNumber(Math.floor You're better off with something like https://jsfiddle.net/9h7dqgzk/2/ geeves fucked around with this message at 04:04 on Nov 12, 2016 |
# ¿ Nov 12, 2016 04:01 |
|
ddiddles posted:One of the best courses I took (well, still taking) was Anthony Alicea's JS Udemy course, he takes the time to explain how the JS engine works, which sheds a lot of light on how the actual syntax works. The first 3.5 hours on on youtube (Alicea's actual youtube account it seems) if you want to take more of peek to see if it's worth the $20. https://www.youtube.com/watch?v=Bv_5Zv5c-Ts
|
# ¿ Nov 29, 2016 22:46 |
|
The Fool posted:I made a terrible thing last night. Roadie posted:Nothing. Anything that lets people run arbitrary command-line code will gently caress you. Exactly. #1 in java script: The Bad Parts or "DON'T loving DO THIS" - exec() (I realize this may be different in node, than in the browser - but until I know better/....) If you are running a .exe (or .sh) make sure it's something airtight that you have also written yourself and you know exactly what you will be executing. If you have to write sketchy code like this perhaps you should be handing off to another service / queuing system that can handle it properly. Not to enable you (I don't know what powershell.exe does) at the very least, you should be checking if req.params.command are in fact valid and what you want to allow (for example, if it does CRUD and you only want Read, make sure the params are Read params - otherwise 403 / 500 that response). Treat everything like you would SQL injection and don't pass them blindly. But as Roadie said, "No".
|
# ¿ Dec 16, 2016 01:48 |
|
necrotic posted:https://www.npmjs.com/package/money-math yeah. Just don't. when I had to do money-like numbers, I just sent it to the server instead. Much safer and not much longer.
|
# ¿ Jan 1, 2017 02:55 |
|
Bruegels Fuckbooks posted:I was thinking about replying like this but then I remembered that node.js is a thing, so it's conceivable that node.js could be used by like banking sites or some poo poo. The next solo Superman movie will be a remake of 3 and have Kevin hart as the villain working with Lex. He won't be collecting simple half pennies from banks. Who knows what he'll exploit with node.js. I highly doubt any financial institution is using node.js for any calculation.
|
# ¿ Jan 4, 2017 15:25 |
|
Sab669 posted:(Posting from my phone at work, sorry for poor formatting.) JavaScript is a little weird with tables and childNodes. Technically each table has a <tbody> and <thead> I believe, even if you do not declare them. Best to go after them explicitly: (Edit: also there are technically 9 childNodes of <tr> in this example, not 4) https://jsfiddle.net/createanaccount/z56yesz0/ geeves fucked around with this message at 14:47 on Feb 7, 2017 |
# ¿ Feb 7, 2017 14:38 |
|
There Will Be Penalty posted:Use table_element.querySelector("> tbody > tr > td:nth-child(even)") This is much better. I keep forgetting that querySelector exists half the time.
|
# ¿ Feb 8, 2017 15:03 |
|
|
# ¿ Apr 29, 2024 12:05 |
|
Dominoes posted:The JS economy feels confusing if you don't use Node, since most guides assume you're using it. Is there a well-written guide on how to get started with Node / Bower? More so on the Bower end as we don't use node.js. Or is there something other than Bower? Or is this a Grunt / webpack thing to combine all of our JS dependencies? There's 50 tools out there and looking at the JS community it's maddening coming from a community that basically has 3 solid, well developed tools for building an application (Gradle, Maven and Ant). The dev who introduced us to all of this a couple of years ago just did a really piss poor job of it because all he really wanted was grunt watch. He basically would install packages via bower then just copy them into our repository instead of actually using its process. We're well behind the times and I've been knee deep in moderizing our Java build process and I want to continue this on the front end side of things. You're correct about every React / Redux just making assumptions and not at least writing, first you need to install X - follow these instructions on this site or something.
|
# ¿ Mar 30, 2017 22:12 |