|
Speaking of Typescript: what's the best, most elegant way to check and assign a value that might be undefined?code:
|
# ? Nov 25, 2020 10:30 |
|
|
# ? Jun 6, 2024 11:56 |
|
Dominoes posted:Typescript is an improvement to the language, but requires a build step. If your process has one anyway, use it. If not, balance easier-to-use code with skipping the build and config TS requires. Hell yeah, front end build tools. Because there aren't enough of them. fsif posted:Look if you're just making a website that needs a hamburger menu and maybe a modal or two you can absolutely (and probably should) use vanilla JS. There's no need to overengineer every project that touches JS. Comedy answer: jQuery
|
# ? Nov 25, 2020 12:03 |
|
uncle blog posted:Speaking of Typescript: what's the best, most elegant way to check and assign a value that might be undefined? If you’re using at least TypeScript 3.7 you can use optional chaining: code:
|
# ? Nov 25, 2020 12:11 |
|
nexus6 posted:Comedy answer: jQuery The biggest thing I wanted from jQuery exists in vanilla now and I love it $ -> querySelector $$ -> querySelectorAll
|
# ? Nov 25, 2020 12:15 |
|
IAmKale posted:If you’re using at least TypeScript 3.7 you can use optional chaining: Thanks, but we're still at 3.5 I'm afraid.
|
# ? Nov 25, 2020 13:07 |
|
A Cup of Ramen posted:don't suppose anyone would have any course recommendations that cover JS really well would they? freecodecamp had a lot of really good stuff back when i used it
|
# ? Nov 25, 2020 13:14 |
Biowarfare posted:The biggest thing I wanted from jQuery exists in vanilla now and I love it How about AJAX? I'll be writing a site and thinking "Hmm I should do this in vanilla" and then realize I have five hundred AJAX calls to do. Every time I look up "how to do ajax in vanilla JS" it basically tells me to write my own XMLHttpRequest.onreadystatechange handlers like a loving caveman
|
|
# ? Nov 25, 2020 13:16 |
|
Data Graham posted:How about AJAX? I'll be writing a site and thinking "Hmm I should do this in vanilla" and then realize I have five hundred AJAX calls to do. Every time I look up "how to do ajax in vanilla JS" it basically tells me to write my own XMLHttpRequest.onreadystatechange handlers like a loving caveman http://youmightnotneedjquery.com/ https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
|
# ? Nov 25, 2020 13:36 |
|
I understand that jquery is obsolete now that we have better things, but theres still reasons to use it, and is a upgrade from "vanilla js". God knows javascript is a terrible language,, and browsers are a idiots environment. The frameworks arms race started because vanilla js over a browser can be a miserable experience.
|
# ? Nov 25, 2020 13:39 |
|
Tei posted:I understand that jquery is obsolete now that we have better things, Also, jQuery doesn't require you to compile/transpile your code. You can just drop it in.
|
# ? Nov 25, 2020 13:40 |
|
Tei posted:but theres still reasons to use it, and is a upgrade from "vanilla js". yes, if you are not interested in keeping up with contemporary technology, are a self-proclaimed "back-end only!" developer with no interest in learning any front-end technologies at all, or if you have been using jquery for the last 45 years anyway and just don't want to learn new stuff. those are (relatively) valid reasons. javascript has gotten a lot better since es2015. there's a reason why so many people use it and like it. if you don't need a lot of javascript (and you should absolutely not be using javascript unless you actually have to, despite what some folks might tell you) not using jquery is especially useful, because jquery still has to be downloaded, (possibly) uncompressed and parsed. sure it might be fast for most people, but it's not free. some of the most common usecases are even in vanilla js now: code:
code:
code:
code:
|
# ? Nov 25, 2020 13:57 |
Sweet. I see that it means more boilerplate than $.ajaxSetup if I want to (say) add a CSRF token header into every call, but that's what the Django docs say to do now (replacing their previous jQuery-centric docs), so it's clear which way the wind is blowing.
|
|
# ? Nov 25, 2020 14:11 |
|
Data Graham posted:that's what the Django docs say to do now Dominoes fucked around with this message at 14:20 on Nov 25, 2020 |
# ? Nov 25, 2020 14:17 |
Yeah, seriously. Like yeah I want user management out of the box. And it feels like its momentum is picking up if anything, not slacking.
Data Graham fucked around with this message at 14:24 on Nov 25, 2020 |
|
# ? Nov 25, 2020 14:22 |
|
uncle blog posted:Thanks, but we're still at 3.5 I'm afraid. JavaScript code:
|
# ? Nov 25, 2020 15:28 |
|
That'll probably complain that .prop doesn't exist on {} Try code:
|
# ? Nov 25, 2020 15:32 |
|
prom candy posted:That'll probably complain that .prop doesn't exist on {} The "something" variable is just typed as a string or undefined. Perfectly valid Also here's the babel plugin if for some reason you can't upgrade: https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining
|
# ? Nov 25, 2020 16:45 |
|
Oh nice, I swear I used to try to do those kinds of substitutions and the compiler would always yell at me, maybe that was with an older version or a different configuration though
|
# ? Nov 25, 2020 18:42 |
|
uncle blog posted:Speaking of Typescript: what's the best, most elegant way to check and assign a value that might be undefined? I would probably go with: JavaScript code:
JavaScript code:
|
# ? Nov 25, 2020 20:59 |
|
jQuery still has its place but modern javascript has adopted so much of the good stuff from it it's really only needed for a few things. I do find it funny that the main thing jQuery is made fun of (being used in instances where it's not strictly necessary, bogging down websites that don't need it) applies just as much to pretty much every other modern front-end framework, but jQuery is still the most maligned.
|
# ? Nov 25, 2020 22:02 |
|
Data Graham posted:Yeah, seriously. Like yeah I want user management out of the box. And it feels like its momentum is picking up if anything, not slacking. Yeah, it's pretty great, and fairly trivially extensible if you can't do exactly what you want with the built-in functionality.
|
# ? Nov 25, 2020 22:21 |
|
I find the => operator ugly
|
# ? Nov 25, 2020 22:34 |
|
I'll never understand anyone who turns up their nose at TypeScript. You can use as few or as many of its features as you want; you don't even need to change how you write code, 95% of types can be inferred automatically. Even in pre-existing codebases you can easily configure vanilla JS and TS to play nice together, you don't have to rewrite anything. TypeScript has saved me magnitudes more time and headaches than it costs to install and configure it, helps keep code "self documenting", and helps me think about my data structures and organization more intentionally.
|
# ? Nov 25, 2020 22:35 |
|
Null of Undefined posted:jQuery still has its place but modern javascript has adopted so much of the good stuff from it it's really only needed for a few things. Yeah, modern JS requires 16 million dependencies that you install with npm.
|
# ? Nov 25, 2020 23:09 |
|
Null of Undefined posted:jQuery still has its place but modern javascript has adopted so much of the good stuff from it it's really only needed for a few things. There was a point in the distant past where JQuery was a useful - back when having to support IE6 was useful. Then we reached a certain point where browsers could do natively most of what JQuery was doing, and using JQuery for say, event handlers would actually solve no problems and introduce new ones - e.g. on some browsers, a mousemove event bound with jquery would spend about 70% CPU time in "event normalization" (e.g. taking the native event objects and converting them into an internal JQuery form to try to minimize the browser differences in event handling.) Most of the features the library introduced (e.g. the JQuery selector library) were horrifically slow and cache after cache was tacked on in order to get them to function with some appropriate degree of performance, and the caches would introduce memory leaks, etc. One of the big reasons AngularJS was thrown out for Angular2 turned out to be this JQuery/JQLite dependency - even the small subset of JQuery present in JQLite was bad enough for performance that they had to effectively start from scratch. Even the features that work that aren't slow suck. JQuery promises for instance are usable, but suck (https://thewayofcode.wordpress.com/2013/01/22/javascript-promises-and-why-jquery-implementation-is-broken/. I mean, if you have to slap together something quick that needs to run in an old IE browser, I wouldn't knock JQuery. But it's caused more problems than it solves for many years now.
|
# ? Nov 25, 2020 23:53 |
Something I've been doing lately when I can't sleep is dig up old projects from like 2009 that I had written in ColdFusion or Perl/CGI or some poo poo and update them to be on something modern and sane. In the process I'm taking the opportunity to try to excise jQuery if at all possible. Now that I know about fetch and the various querySelector things, I think the only areas where it's not possible to get rid of it are the plugins I have on these sites like carousels and such that are themselves based on jQuery. I'd imagine those are going to be the last things to get their installed-base bulk out from under the jQuery inertia.
|
|
# ? Nov 26, 2020 00:06 |
|
I've been maintaining an Angular app that allows employees to clock in via a web portal, and some of them are seeing the time appear incorrectly. First off, I've added a bit of logging to try to get some visibility into the problem and confirm my suspicion; that some of their browsers are using an incorrect timezone. My first question, just so that I know I'm on the right track, is whether or not this line of code is telling me what I think it is: code:
My second question is how this actually happens, or more importantly how the user can correct this. The offending browser is Chrome 81 for Android, and as far as I can tell there's no way to view or change the timezone; it's just set automatically using the system settings (yet the clock for this device is actually set correctly). Finally, since all the users are in the same timezone, just hard coding the app to display correctly for the specified timezone is also an option. Do Angular date pipes provide a way to do this? Ideally, this would accommodate Daylight Savings, so that this doesn't break again in the spring.
|
# ? Nov 26, 2020 03:46 |
|
Data Graham posted:Something I've been doing lately when I can't sleep is dig up old projects from like 2009 that I had written in ColdFusion or Perl/CGI or some poo poo and update them to be on something modern and sane. In the process I'm taking the opportunity to try to excise jQuery if at all possible. Most WordPress form managers are built with jQuery, and they're too useful to not have. I've wanted to create my own form manager that's basically Contact Form 7 that actually works reliably and stores submissions, but
|
# ? Nov 26, 2020 03:58 |
|
New Coke posted:I've been maintaining an Angular app that allows employees to clock in via a web portal, and some of them are seeing the time appear incorrectly. First off, I've added a bit of logging to try to get some visibility into the problem and confirm my suspicion; that some of their browsers are using an incorrect timezone. Time zones are a special circle of hell. If you have the option to, never use native js Dates anywhere and use a library like Luxon. If you can’t, and you are 100% sure the users are all in the same time zone, look into using the toLocale[Date | Time]String methods on Date where you can set the timeZone option to force it to render in a given zone that should handle DST and all that fun stuff. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
|
# ? Nov 26, 2020 05:01 |
|
Tei posted:I find the => operator ugly I like you Tei but your takes today are absolutely wild
|
# ? Nov 26, 2020 05:24 |
|
Lumpy posted:Time zones are a special circle of hell. If you have the option to, never use native js Dates anywhere and use a library like Luxon. If you can’t, and you are 100% sure the users are all in the same time zone, look into using the toLocale[Date | Time]String methods on Date where you can set the timeZone option to force it to render in a given zone that should handle DST and all that fun stuff. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString Yeah this 100% use something else if you can. I've had much better success with moment.js. Otherwise you'll have to figure some workaround with the javascript Date api. Like force it to read in UTC and convert or something.
|
# ? Nov 26, 2020 05:34 |
|
Tei posted:I find the => operator ugly
|
# ? Nov 26, 2020 07:40 |
|
prom candy posted:I like you Tei but your takes today are absolutely wild I have a lot of bad opinions. I hope you still like me despite them I programmed with Perl and like terse code, but => is a bit too much. I also like "{" and "}" and is why I don't like Python much. Is just a aesthetic preference, nothing mayor. I want my code a bit more verbose and not too much based on mystery symbols.
|
# ? Nov 26, 2020 10:18 |
fireraiser posted:Have you tried any fonts with programming ligatures, like FiraCode or Hasklig? Oh poo poo is that what those websites demonstrating these kinds of operators are doing. I’m like reading a thing that says “yeah just use a 📎 b” and thinking well that’s going to present some problems
|
|
# ? Nov 26, 2020 15:33 |
|
Tei posted:I have a lot of bad opinions. I hope you still like me despite them I just find it so much easier to type, especially for inline anonymous functions. code:
|
# ? Nov 26, 2020 17:17 |
|
You can always set up eslint to demand (alwaysparens) => { return alwaysbraces }, and make it --fixable
|
# ? Nov 26, 2020 18:23 |
|
Forcing no implicit return would be getting rid of one of the nicest features of ES6 though
|
# ? Nov 26, 2020 22:43 |
|
prom candy posted:I like you Tei but your takes today are absolutely wild haha word I love =>
|
# ? Nov 28, 2020 05:34 |
|
New Coke posted:I've been maintaining an Angular app that allows employees to clock in via a web portal, and some of them are seeing the time appear incorrectly. First off, I've added a bit of logging to try to get some visibility into the problem and confirm my suspicion; that some of their browsers are using an incorrect timezone. You should be handling all this time stuff on the server side. Never trust the client. Make an endpoint that the client posts to that simply indicates the user clocked in. Get the time on the server when the request comes in and store it in your database in UTC. When you display that time to a user, send it to the client in UTC and use the client time zone to translate it, at which point the previous suggestions about luxon and moment become relevant.
|
# ? Nov 29, 2020 05:11 |
|
|
# ? Jun 6, 2024 11:56 |
|
Chenghiz posted:You should be handling all this time stuff on the server side. Never trust the client. Make an endpoint that the client posts to that simply indicates the user clocked in. Get the time on the server when the request comes in and store it in your database in UTC. When you display that time to a user, send it to the client in UTC and use the client time zone to translate it, at which point the previous suggestions about luxon and moment become relevant. Just an FYI, moment is no longer being supported in favor of Luxon. Just in case that helps anyone looking at libraries for the first time. First time, get it!
|
# ? Nov 29, 2020 05:41 |