Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Strong Sauce posted:

where whatever code in those functions are determined by the response time of the IO being called. then yes, i agree, and that is essentially what i said.
Yeah, wasn't trying to explain to you as if you didn't get it, just trying to minimize the confusion your phrasing would cause for someone who was already having trouble understanding the ordering of things.

Adbot
ADBOT LOVES YOU

smackfu
Jun 7, 2004


Oh yeah, a stack overflow question and answer from 2010. That’s probably good JavaScript advice in TYOOL 2019.

quote:

using the Date object is just an overkill, and should be avoided since the ISO-8601 format is not widely supported -yet-,

quote:

Hint: Try it on IE8 or below, and even in old Firefox versions, it simply will not work..

necrotic
Aug 2, 2005
I owe my brother big time for this!
Moment is mutable all the way. If I have to use moment I'm in the habit of cloning the object before mutating because of that. It's really frustrating to work with .

Chenghiz
Feb 14, 2007

WHITE WHALE
HOLY GRAIL
There is an immutable version of moment called Luxon.

MrMoo
Sep 14, 2000

Chenghiz posted:

There is an immutable version of moment called Luxon.

Thanks for that, I managed to update a bit of my NYSE work to now actually work in other timezones. It is rather unfortunate that neither Moment or Luxon appear to be able to provide seconds-past-the-epoch local to a timezone. I guess it's a bogus entity but it is a rather popular one.

I need to basically work in seconds-past-the-epoch in NYT anywhere in the world, but the APIs only support UTC underneath.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



FSMC posted:

I pretty sure I would have used a for loop if it wasn't for the warning from eslint against using for loops.

Are you sure it's not a warning about using for in loops? Those have some behavior that people find surprising, so a lot of people think they shouldn't be used, and, now that we have for of which has less surprising behavior, they're mostly right.

TIP
Mar 21, 2006

Your move, creep.



smackfu posted:

Oh yeah, a stack overflow question and answer from 2010. That’s probably good JavaScript advice in TYOOL 2019.

I was just answering your question, "how do you even come up with that?" Your junior dev googled it and used the top answer on Stack Overflow.

I wasn't saying, "it's the top answer on stack overflow, therefore the most correct!"

smackfu
Jun 7, 2004

Tip posted:

I wasn't saying, "it's the top answer on stack overflow, therefore the most correct!"

Didn’t think that at all, I was just rolling my eyes at the whole stack overflow page. Advice not to use a standard does not age well.

geeves
Sep 16, 2004

Tip posted:

I was just answering your question, "how do you even come up with that?" Your junior dev googled it and used the top answer on Stack Overflow.

I wasn't saying, "it's the top answer on stack overflow, therefore the most correct!"

You cannot downvote without 125 reputation!

Anony Mouse
Jan 30, 2005

A name means nothing on the battlefield. After a week, no one has a name.
Lipstick Apathy
Apparently date-fns is all the rage though I haven’t tried it myself yet.

Chenghiz
Feb 14, 2007

WHITE WHALE
HOLY GRAIL

Anony Mouse posted:

Apparently date-fns is all the rage though I haven’t tried it myself yet.

It's nicer than moment but it doesn't do time zone conversions or durations, so not much use if you need those things.

Roadie
Jun 30, 2013

geeves posted:

You cannot downvote without 125 reputation!

Also, that time you tried to make a post with a better answer, they immediately closed it as a duplicate and redirected people to the bad answer.

MrMoo posted:

Thanks for that, I managed to update a bit of my NYSE work to now actually work in other timezones. It is rather unfortunate that neither Moment or Luxon appear to be able to provide seconds-past-the-epoch local to a timezone. I guess it's a bogus entity but it is a rather popular one.

I need to basically work in seconds-past-the-epoch in NYT anywhere in the world, but the APIs only support UTC underneath.

Luxon and Moment are both internally based on an epoch timestamp. All the time zone handling is purely for parse-to-timestamp and parse-from-timestamp purposes.

JavaScript code:
import { DateTime } from 'luxon';

DateTime.fromMillis(0).year; // 1970
DateTime.fromSeconds(0).year; // 1970

SnatchRabbit
Feb 23, 2006

by sebmojo
I'm working on a navigation/management page for some of my AWS resources. I had this code chunk working properly but our CSS guy went and made some changes and now its broken and I have no idea why. It looks like all the source files are the same as well as the div ids and the javascript, but when I run this chunk of code with the array data of [0: "crm", 1: "cs", 2: "elm", 3: "es", 4: "fscm", 5: "hcm", 6: "ih"] it returns an error. Essentially, I am querying an S3 bucket for a list of folders and I want to have each folder added as an option to the select. It worked find before so I'm not sure why it's broken now.

code:
            <div id="newenvapp">
                <select id="newenvapp1" style="width:100px;" name="newenvapp1" onChange="OnSelectedAppChange()"></select>
            </div>
code:
                    var select = document.getElementById("newenvapp1");
                    for(var count = 0; count < data.length; count++){
                        var option = document.createElement('option');
                        option.text = option.value = data[count];
                        console.log(option.value);
                        select.add(option, 0);
I get this error:

code:
Uncaught TypeError: Cannot read property 'add' of null
    at Object.success (jumppage.html:121)
    at fire (VM74 jquery-3.3.1.js:3268)
    at Object.fireWith [as resolveWith] (VM74 jquery-3.3.1.js:3398)
    at done (VM74 jquery-3.3.1.js:9305)
    at XMLHttpRequest.<anonymous> (VM74 jquery-3.3.1.js:9548)
edit: i figured it out, the css and mustache templates I was using weren't loading in time for the function to kick off.

SnatchRabbit fucked around with this message at 20:44 on Feb 13, 2019

huhu
Feb 24, 2006

SnatchRabbit posted:

I had this code chunk working properly but our CSS guy went and made some changes and now its broken and I have no idea why.
Sounds like you're not using source control. Why are you not using source control?

Duct Tape
Sep 30, 2004

Huh?
I'm fiddling around with building a media player in Electron, but short of the <video> attribute, I don't see any way of rendering video. While <video> is great and all, there are a boatload of formats and codecs that won't play in it.

I can easily bundle in a copy of ffplay, which handles almost universal playback no problem, but the app launches in a separate window when run from childProcess.exec. That's to be expected but it's not what I want.

Is there a way I can, I don't know, hide the child process and copy it's output frame-by-frame into my app? Or nest the process inside Electron somehow? The best solution I've been able to find so far is transcoding it on the fly to a format that <video> supports, but that's not something I'd like to do. Anyone have any suggestions?

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop

huhu posted:

Sounds like you're not using source control. Why are you not using source control?

I just started developing on GitHub as an individual and holy smokes where has this workflow been my whole life. I've discovered the concept of "branch per feature", which basically lets me break my project's code or entire API in ten or a hundred different ways at once, independently, without having to hesitate or worry about what breaking it one way will mean and how long it will delay me from working on any other features while it's broken. No, with GitHub I can just start a new branch any time and break the code to pieces just to experiment. I can access a non-broken copy that works any time just by switching the selected branch to the main unbroken one.

The transition is hilariously smooth too. Just select a version (each representing a potential feature or rewrite direction) from the dropdown in GitHub desktop and instantly all the correct files teleport into place in your file system. I don't even have to close out Chrome, Chrome debugger, or my python host when this happens; they all see the correct codebase of whatever code branch I click on the instant I decide to change what I'm working on.

Roadie
Jun 30, 2013

Dumb Lowtax posted:

I just started developing on GitHub as an individual and holy smokes where has this workflow been my whole life. I've discovered the concept of "branch per feature", which basically lets me break my project's code or entire API in ten or a hundred different ways at once, independently, without having to hesitate or worry about what breaking it one way will mean and how long it will delay me from working on any other features while it's broken. No, with GitHub I can just start a new branch any time and break the code to pieces just to experiment. I can access a non-broken copy that works any time just by switching the selected branch to the main unbroken one.

The transition is hilariously smooth too. Just select a version (each representing a potential feature or rewrite direction) from the dropdown in GitHub desktop and instantly all the correct files teleport into place in your file system. I don't even have to close out Chrome, Chrome debugger, or my python host when this happens; they all see the correct codebase of whatever code branch I click on the instant I decide to change what I'm working on.

If you really want fuckin' magic, start playing around with Github Actions or other CI (Continuous Integration) platforms like Travis CI or Circle CI. You can do stuff like have version numbers and changelogs automatically generate from git commits, have tests automatically run whenever branches get updated with results posted to pull requests, or even have code automatically bundle and deploy to a server whenever master gets updated without having to do anything manually.

Also, if you start experimenting with the git command line, make sure to look at git add -p (lets you add only part of a changed file instead of the whole thing) and git rebase -i (lets you play with the history of local commits, so you can do stuff like take a bunch of commits named "WIP changes" and merge them into a single one).

Roadie fucked around with this message at 05:16 on Feb 16, 2019

Obfuscation
Jan 1, 2008
Good luck to you, I know you believe in hell
I've been using [...Array(x)] as a hacky way to initialize empty arrays but this doesn't work in typescript. Is there a better way?

necrotic
Aug 2, 2005
I owe my brother big time for this!
Array(x).fill(undefined)

Dunno if it will actually work with undefined as the arg but I don't see why it wouldn't.

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Obfuscation posted:

I've been using [...Array(x)] as a hacky way to initialize empty arrays but this doesn't work in typescript. Is there a better way?

edit nm

Obfuscation
Jan 1, 2008
Good luck to you, I know you believe in hell

necrotic posted:

Array(x).fill(undefined)

Dunno if it will actually work with undefined as the arg but I don't see why it wouldn't.

It does work, thanks.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
I'm trying to make an object with default data members that the constructor can override if it likes. Something like this:

code:
class M
{ constructor( { a = 0, b = 1, c = 1, d = 40 } = {} )
  { Object.assign( this, arguments );
  }
}

new M().a     // Should return 0
The problem is, it doesn't work. The "arguments" object is empty.

I'm sure there's something I could do by making the function signature complex as hell but I want to avoid making it unreadable; the point is to clearly show what data members the object expects to have.

I also, of course, want to avoid typing out all the keys again like this:

code:
class M
{ constructor( { a = 0, b = 1, c = 1, d = 40 } = {} )
  { Object.assign( this, { a, b, c, d } );
  }
}
Because I'm going to define a ton of these objects and the code needs to stay flexible. Is there any syntax that does what I want?

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Dumb Lowtax posted:

I'm trying to make an object with default data members that the constructor can override if it likes. Something like this:

code:
class M
{ constructor( { a = 0, b = 1, c = 1, d = 40 } = {} )
  { Object.assign( this, arguments );
  }
}

new M().a     // Should return 0
The problem is, it doesn't work. The "arguments" object is empty.

I'm sure there's something I could do by making the function signature complex as hell but I want to avoid making it unreadable; the point is to clearly show what data members the object expects to have.

I also, of course, want to avoid typing out all the keys again like this:

code:
class M
{ constructor( { a = 0, b = 1, c = 1, d = 40 } = {} )
  { Object.assign( this, { a, b, c, d } );
  }
}
Because I'm going to define a ton of these objects and the code needs to stay flexible. Is there any syntax that does what I want?

Tired phone posting, but something like this maybe?
JavaScript code:
class M {
  constructor (options) {
    const _default = {a:1,b:2,...}
    const _merged = Object.assign(_default, options)
    for (const k in _merged){
      this[ k ] = _merged[ k ]
    }
  }
}

Lumpy fucked around with this message at 05:36 on Feb 17, 2019

Roadie
Jun 30, 2013

Dumb Lowtax posted:

I also, of course, want to avoid typing out all the keys again like this:

code:
class M
{ constructor( { a = 0, b = 1, c = 1, d = 40 } = {} )
  { Object.assign( this, { a, b, c, d } );
  }
}
Because I'm going to define a ton of these objects and the code needs to stay flexible. Is there any syntax that does what I want?

JavaScript code:
class M {
  a = 0;
  b = 1;
  c = 1;
  d = 40;

  constructor(args = {})  {
    Object.assign(this, args)
  }
}
Make sure to have @babel/plugin-proposal-class-properties, though.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
Can't compile it with anything, this code is for students and it's best if we stick to raw JavaScript.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
Does anyone know why the arguments object is empty there? Maybe es6 object destructuring syntax in parameters is like a new and weird part of the language, and they're phasing out the arguments keyword anyway so maybe they just didn't bother making the two things work together?

smackfu
Jun 7, 2004

Isn’t arguments more like an array than an object?

Doom Mathematic
Sep 2, 2008
Yes, I'd avoid writing anything like constructor (args) { ... }, because it confuses matters by giving the first entry in the arguments array (well, array-like object) the name args.

Anyway:

JavaScript code:
const defaults = {
  a: 0,
  b: 1,
  c: 1,
  d: 40
}

class M {
  constructor(props = {})  {
    Object.assign(this, defaults, props)
  }
}

Doom Mathematic
Sep 2, 2008

Dumb Lowtax posted:

Does anyone know why the arguments object is empty there? Maybe es6 object destructuring syntax in parameters is like a new and weird part of the language, and they're phasing out the arguments keyword anyway so maybe they just didn't bother making the two things work together?

This isn't to do with object destructuring, it's more to do with the default parameter syntax. The arguments object you get automatically in any function is apparently derived from what the caller originally passes in, not from what your default parameter syntax creates.

JavaScript code:
const f = function (a, b, c = 7, d = 8) {
  console.log(a, b, c, d) // 1 2 7 8
  console.log(arguments) // [Arguments] { '0': 1, '1': 2 }
}

f(1, 2)
In the case new M() the caller is passing no parameters, so the arguments object is empty.

Doom Mathematic fucked around with this message at 16:05 on Feb 17, 2019

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop

Doom Mathematic posted:

This isn't to do with object destructuring, it's more to do with the default parameter syntax. The arguments object you get automatically in any function is apparently derived from what the caller originally passes in, not from what your default parameter syntax creates..

Ah, makes sense. Thanks!

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
On the other hand it feels like object destructuring syntax is kind of useless if there's no clean way to do the above.

Why even add something like default function arguments to es6 if you can't use the new syntax with it? Why not make it possible to capture the arguments object *after* the default arguments are applied, since it's still common to want a guaranteed way of listing them all. Such as in my use case above.

Happy Thread fucked around with this message at 23:09 on Feb 17, 2019

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
Right now the best option seems to be the following. It completely omits object destructuring in parameters, which is really odd since it seems like that syntax was pretty much made for this situation and yet can't do the most useful thing. It also sucks because it's harder for the reader to immediately see what the constructor wants.

code:
class M
{ constructor( options )               // Previously:  constructor( { a = 0, b = 1, c = 1, d = 40 } = {} )
  {  // Now the reader has to find the defaults in the function body instead:
    Object.assign( this, { a = 0, b = 1, c = 1, d = 40 } )
    Object.assign( this, options );
  }
}

necrotic
Aug 2, 2005
I owe my brother big time for this!
Destructioring an object with defaults doesn't change the right hand side. Arguments is a reference to what the user passed in, which isn't being modified.

Roadie
Jun 30, 2013
The "right" answer for this kind of thing is the class property stuff with default values, anyway, which is stage 3 and only isn't in browsers yet because tc39 delayed it to also include private properties at the same time.

Like, "no build step" to not overwhelm students is well and good, but at some point you have to take into account the "making half of the stuff in the Javascript world work in a not-ridiculous way requires a build step because of browser incompatibilities or stuff that isn't rolled out yet" problem.

geeves
Sep 16, 2004

necrotic posted:

Destructioring an object with defaults doesn't change the right hand side. Arguments is a reference to what the user passed in, which isn't being modified.

Destructuring defaults is also excellent to help avoid null and undefined.

Some JS devs want to argue about not having to use types, etc. in their code because it makes it too verbose and unclean, yet write code full of if / else malarkey checking for null values.

If JS or TypeScript can start implementing the "?." (I forget its formal name) for null safety like Kotlin, I'd be very happy.

Roadie
Jun 30, 2013

geeves posted:

If JS or TypeScript can start implementing the "?." (I forget its formal name) for null safety like Kotlin, I'd be very happy.

That's in stage 1, and there's a Babel plugin for it.

Doom Mathematic
Sep 2, 2008

Dumb Lowtax posted:

Right now the best option seems to be the following. It completely omits object destructuring in parameters, which is really odd since it seems like that syntax was pretty much made for this situation and yet can't do the most useful thing. It also sucks because it's harder for the reader to immediately see what the constructor wants.

code:
class M
{ constructor( options )               // Previously:  constructor( { a = 0, b = 1, c = 1, d = 40 } = {} )
  {  // Now the reader has to find the defaults in the function body instead:
    Object.assign( this, { a = 0, b = 1, c = 1, d = 40 } )
    Object.assign( this, options );
  }
}

For the use case you described, there is no need to destructure the incoming options object, either in parameters or otherwise.

I'm actually a bit puzzled as to why you prefer this solution over the one I suggested. Why two Object.assign calls? Why not keep the defaults as a distinct object? Another variant:

JavaScript code:
class M {
  constructor (options) {
    Object.assign(this, this.constructor.defaults, options)
  }
}

M.defaults = { a: 0, b: 1, c: 1, d: 40 }

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
Don't worry I did it your way Doom, just had a memory blip about how it was. Except I threw those defaults in as local to the constructor just to keep it all contained.

geeves
Sep 16, 2004

Roadie posted:

That's in stage 1, and there's a Babel plugin for it.

:allears:

Adbot
ADBOT LOVES YOU

Polio Vax Scene
Apr 5, 2009



I want to use Notifications in my site, but some dumb script is overwriting the Notification object as defined by the browser. What can I do about this if I can't remove the script?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply