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
Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Osmosisch posted:

The nice thing about lodash is that it lets you do all this stuff without worrying about which browser/platform you're targeting too much.

Thats (one of) the nice thing about transpiling too.

Adbot
ADBOT LOVES YOU

teen phone cutie
Jun 18, 2012

last year i rewrote something awful from scratch because i hate myself
What's a solid place to learn the MERN stack, given I'm already pretty good with React?

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.
Got another "I should really know this by now" javascript question. See this code:

code:
export default class Terminal extends React.Component {

    componentDidMount() {
        this.term = new TerminalWidget(opts);
        on('disconnect', this.onDisconnect);  //on is an imported function that works appropriately, basically just an event handler delegate
    }

    onDisconnect() {
        this.term.write("Disconneced!");
        setTimeout(() => {
            this.term.destroy();
        }, 5000);
    }
}
However, after the component is mounted and the disconnect is fired I get

code:
Uncaught TypeError: Cannot read property 'term' of null
I thought because the function I am defining is defined inside the class it implicitely gets "this" of the whole class. I may be loving up scope again...

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Grump posted:

What's a solid place to learn the MERN stack, given I'm already pretty good with React?

Unless its for a particular job or something, I wouldn't bother since MongoDB is poo poo.

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

Knifegrab posted:

Got another "I should really know this by now" javascript question. See this code:

code:
export default class Terminal extends React.Component {

    componentDidMount() {
        this.term = new TerminalWidget(opts);
        on('disconnect', this.onDisconnect);  //on is an imported function that works appropriately, basically just an event handler delegate
    }

    onDisconnect() {
        this.term.write("Disconneced!");
        setTimeout(() => {
            this.term.destroy();
        }, 5000);
    }
}
However, after the component is mounted and the disconnect is fired I get

code:
Uncaught TypeError: Cannot read property 'term' of null
I thought because the function I am defining is defined inside the class it implicitely gets "this" of the whole class. I may be loving up scope again...

If it is called from the class, yes. If the callback is called from another context, 'this' changes. You used an arrow function in the timeout, so that is safe. However, if the object itself is GCed, and then your timeout function tries to run, it would not be able to access this.term.

teen phone cutie
Jun 18, 2012

last year i rewrote something awful from scratch because i hate myself

Thermopyle posted:

Unless its for a particular job or something, I wouldn't bother since MongoDB is poo poo.

Yeah i see a lot of jobs that require node experience, so i figured learning that stack together might be helpful.

Are there better alternatives? I was so bored at work today I ran through a basic Java tutorial. I need something to learn

IAmKale
Jun 7, 2007

やらないか

Fun Shoe

Skandranon posted:

If it is called from the class, yes. If the callback is called from another context, 'this' changes. You used an arrow function in the timeout, so that is safe. However, if the object itself is GCed, and then your timeout function tries to run, it would not be able to access this.term.
I think this can be fixed with a second arrow function when calling on():
code:
on('disconnect', () => this.onDisconnect());

Munkeymon
Aug 14, 2003

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



Knifegrab posted:

I thought because the function I am defining is defined inside the class it implicitely gets "this" of the whole class. I may be loving up scope again...

Member functions don't auto-bind to the object instance. The class syntax sugar is an elaborate trap made to make you think otherwise, so don't feel too bad about it.

reversefungi
Nov 27, 2003

Master of the high hat!
Maybe try this?:

code:
    onDisconnect = () => {
        this.term.write("Disconneced!");
        setTimeout(() => {
            this.term.destroy();
        }, 5000);
    }
If you're not using create-react-app that enables arrow functions in classes, then you can do this if you don't want to futz around with Babel:


code:
export default class Terminal extends React.Component {
    constructor() {
 	super();
	this.onDisconnect = this.onDisconnect.bind(this);
    }
}

reversefungi fucked around with this message at 20:32 on Sep 6, 2017

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.

IAmKale posted:

I think this can be fixed with a second arrow function when calling on():
code:
on('disconnect', () => this.onDisconnect());

This will not work since I need a constant reference to the initial function for when I remove that listener at a later point in time.

I realize now my mistake, I was not considering where it would be called. Thanks all for the clarification.


The Dark Wind posted:


code:
export default class Terminal extends React.Component {
    constructor() {
 	super();
	this.onDisconnect = this.onDisconnect.bind(this);
    }
}

This is the solution I came up with as well and appears to work. Thanks all for the help. I swear context will forever be the death of me. That and socket.io's terrible documentation.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Grump posted:

Yeah i see a lot of jobs that require node experience, so i figured learning that stack together might be helpful.

Are there better alternatives? I was so bored at work today I ran through a basic Java tutorial. I need something to learn

There's not much to learning node if you already know JS and used to installing npm packages.

If you're interested, I'd just look up some tutorials on Express.

For a backend database you can't go wrong with Postgres. With the json/jsonb types and queries I guess it can do everything Mongo can do and do it better (though I'd like if someone could point out where this isn't the case).

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

Thermopyle posted:

There's not much to learning node if you already know JS and used to installing npm packages.

If you're interested, I'd just look up some tutorials on Express.

For a backend database you can't go wrong with Postgres. With the json/jsonb types and queries I guess it can do everything Mongo can do and do it better (though I'd like if someone could point out where this isn't the case).

Not only can it do everything Mongo can, but you ALSO have a reliable relational database if, god forbid, you have some relational data.

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.

Thermopyle posted:

There's not much to learning node if you already know JS and used to installing npm packages.

If you're interested, I'd just look up some tutorials on Express.

For a backend database you can't go wrong with Postgres. With the json/jsonb types and queries I guess it can do everything Mongo can do and do it better (though I'd like if someone could point out where this isn't the case).

gently caress yes thank you for recommending postgres I am so sick of people bending over backwards for mongo in situations where it gains them literally no benefit. Postgres is also really really good in so many ways.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.

Skandranon posted:

Not only can it do everything Mongo can, but you ALSO have a reliable relational database if, god forbid, you have some relational data.

The joke here is your data almost always is relational, or becomes relational shortly after you dismiss it as not relational.

I've heard reports that http://knexjs.org is pretty good for working with SQL databases, maybe start there.

Honestly if you ever get stuck working with Mongo you'll be better off with a grounding in what a good database looks like in Node, as Mongo code often ends up with most of the data's schema and implied relations expressed in their code, not in Mongo, and not necessarily in a consistent way.

ROFLburger
Jan 12, 2006

We've used Knex where I work for a while now and it's worked out really well

ROFLburger
Jan 12, 2006

Bookshelf can suck my god drat balls, though

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.
I've got another dumb JS question boys!

Say I am passing a callback to a function, that is expecting two paramters, but I only care about the second, is there some sugar syntax I can do for the the first parameter that effectively means "I don't care what this becomes, I just need to put this here so I can access teh second parameter.

I.E.

code:

doASyncThing((success, err) => processError(err));

Is there anything special I can do so I don't even have to bind success to a variable. I know if I omit it, err becomes the success object, so I need something there. I hope this makes sense.

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

Knifegrab posted:

I've got another dumb JS question boys!

Say I am passing a callback to a function, that is expecting two paramters, but I only care about the second, is there some sugar syntax I can do for the the first parameter that effectively means "I don't care what this becomes, I just need to put this here so I can access teh second parameter.

I.E.

code:

doASyncThing((success, err) => processError(err));

Is there anything special I can do so I don't even have to bind success to a variable. I know if I omit it, err becomes the success object, so I need something there. I hope this makes sense.

No, there is nothing you can do here if your doASyncThing wants 2. I don't see how it could be more succinct than it already is. You could wrap it with doASyncThingErrorOnly, but that basically is what your above line is anyways. Why does that (success, err) bother you so much?

Dogcow
Jun 21, 2005

Knifegrab posted:

code:

doASyncThing((success, err) => processError(err));

Is there anything special I can do so I don't even have to bind success to a variable. I know if I omit it, err becomes the success object, so I need something there. I hope this makes sense.

JavaScript code:
doASyncThing(() => processError(arguments[1]));
:v:

(Don't actually do this, it's ultra terrible)

Sedro
Dec 31, 2008
Borrowing syntax from other languages, you can use underscore
JavaScript code:
doASyncThing((_, err) => processError(err));
But if you want to skip 2 arguments, you'd have to use something else
JavaScript code:
doASyncThing((_, __, err) => processError(err));

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

Sedro posted:

Borrowing syntax from other languages, you can use underscore
JavaScript code:
doASyncThing((_, err) => processError(err));
But if you want to skip 2 arguments, you'd have to use something else
JavaScript code:
doASyncThing((_, __, err) => processError(err));

Don't do this either, this isn't skipping, it's just naming your parameters poorly. (success,err) is the correct thing to do, as if someone comes along later, it's obvious what is going on. Figuring out WTF _ means is a waste of time.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Skandranon posted:

Don't do this either, this isn't skipping, it's just naming your parameters poorly. (success,err) is the correct thing to do, as if someone comes along later, it's obvious what is going on. Figuring out WTF _ means is a waste of time.

Ehh, "_" has a specific meaning in most languages either by convention or by syntax..."the language requires me to put a variable here but we do not have a use for it."

That information is useful to have.

Munkeymon
Aug 14, 2003

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



I usually use whocares, dontcare or whatever but that last one isn't super clear, I guess

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Knifegrab posted:

gently caress yes thank you for recommending postgres I am so sick of people bending over backwards for mongo in situations where it gains them literally no benefit. Postgres is also really really good in so many ways.

I've rediscovered my love for document storage after learning to use views incredibly liberally. It just returns whatever crazy object I want spread out all over my database. But for replication and less of a need for replication reasons, a SQL database is often a better option.

Wish there was a super simple basic SQL database without all the frills of MySQL and Postgres. Like sqlite but not sqlite. Also I would like something better than using blob for file attachments.

Nolgthorn fucked around with this message at 19:31 on Sep 8, 2017

There Will Be Penalty
May 18, 2002

Makes a great pet!

Thermopyle posted:

Ehh, "_" has a specific meaning in most languages either by convention or by syntax..."the language requires me to put a variable here but we do not have a use for it."

That information is useful to have.

Munkeymon posted:

I usually use whocares, dontcare or whatever but that last one isn't super clear, I guess

JSLint recommends that you use the identifier ignore.

This communicates the idea way better.

(I use JSHint these days, which doesn't have that convention.)

ROFLburger
Jan 12, 2006

instead of naming it with hieroglyphics or 'ignoreMeButt', what the hell is wrong with naming it what it is, and just not using it?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

There Will Be Penalty posted:

JSLint recommends that you use the identifier ignore.

This communicates the idea way better.

(I use JSHint these days, which doesn't have that convention.)

Yeah, I agree that a specific name like ignore, unused, etc is more explicit if you're not familiar with the underscore convention/syntax from all the languages that use it.

What you should do is whatever is common practice in surrounding code, in your team, and in the community at large. Unfortunately, despite being over 20 years old the JS community still sucks at having widespread conventions for a lot of stuff.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

ROFLburger posted:

instead of naming it with hieroglyphics or 'ignoreMeButt', what the hell is wrong with naming it what it is, and just not using it?

Naming it success is accurate and (possibly) useful information, but misses the useful information that it is intentionally not used further on. So really, naming it what it is would be something like successButIntentionallyUnused.

necrotic
Aug 2, 2005
I owe my brother big time for this!
I like to prefix unused arguments with _ if I want what it is to be described. Shorter than a long butIgnoreMe suffix, but more descriptive than _ alone.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

The real problem is the lack of a canonical style guide like a lot of other languages have. Or maybe thats not a problem because it gives all of us something to bikeshed over.

ROFLburger
Jan 12, 2006

prefixing with an underscore seems like it would just introduce even more confusion than it's worth because that naming convention normally means something else entirely. I can't imagine being all that confounded by an unused variable but I can certainly see myself getting distracted trying to figure out why someone would name an argument "_" or "_successHandler".

I'm having trouble imagining a scenario where communicating an unused variable would be all that valuable, especially when this is largely a solved problem if you're using modern editors.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

ROFLburger posted:

I'm having trouble imagining a scenario where communicating an unused variable would be all that valuable, especially when this is largely a solved problem if you're using modern editors.

Imagine you're delving into code you don't understand.

ROFLburger
Jan 12, 2006

I'm skeptical you're gaining much value there and if you're delving into code that you "don't understand", this distinction is probably the least of your concerns. But as long as you're not further obfuscating the name with an underscore or whatever I suppose it could only help.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

ROFLburger posted:

I'm skeptical you're gaining much value there and if you're delving into code that you "don't understand", this distinction is probably the least of your concerns. But as long as you're not further obfuscating the name with an underscore or whatever I suppose it could only help.

Well good news! I don't think anyone is claiming you're gaining a lot of value. This whole discussion is bikeshedding.

Dominoes
Sep 20, 2007

In Javascript, the frontier is everywhere. We are bounded only by the browser, and the transpilers, and the ECMAScript committee. The open road still softly calls. It has been said that coding is a humbling and character-building experience. To me, it underscores our responsibility to deal more kindly with one another.

Dominoes fucked around with this message at 16:30 on Sep 10, 2017

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I've been hearing rumblings that REST is dead. That the concept in practice doesn't work very well, that no consistency exists between APIs and that it's basically a crap shoot where the only value derived from it are descriptive urls. But those urls could be replaced easily by descriptive names, in fact names could be those urls or something even more suitable.

Also a point of aggression against REST is that no protocol supports it at all except HTTP and then even methods DELETE and so on are inconsistently implemented. If you have a REST api and want a Websockets version it is flat out impossible.

People are saying a single API endpoint which accepts json GET or POST is the future.

Dominoes
Sep 20, 2007

The concept of a universal framework for transmitting data from a server to interaction-vehicle (Browser, standalone app, phone app etc) is great, regardless of the specific format.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
But I get it. These transmission frameworks are too complicated, in a perfect world I'd be able to simply run a function on a different server. The closest we can come to something so simple is a single parameter in the shape of an object.

Or, even, an array of parameters.

A single endpoint where I can just send something like

code:
{
  "transportId": 2,
  "type": "post:create",
  "content": "hi from the other server"
}
I sorta agree, there is no reason it shouldn't be about that easy. Where transport ids are sent back when the server finishes doing something.

code:
{
  "transportId": 2,
  "type": "post:created",
  "id": 209
}
An api like this could easily be translated into almost any transport protocol.

Dominoes
Sep 20, 2007

I don't know much about REST, but it seems like server takes JSON-like data structure (Python dict, JS object etc), => JSON in transmission => JSON-like datastructure client side. Isn't that what you're asking for?

Adbot
ADBOT LOVES YOU

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

Nolgthorn posted:

I've been hearing rumblings that REST is dead. That the concept in practice doesn't work very well, that no consistency exists between APIs and that it's basically a crap shoot where the only value derived from it are descriptive urls. But those urls could be replaced easily by descriptive names, in fact names could be those urls or something even more suitable.

Also a point of aggression against REST is that no protocol supports it at all except HTTP and then even methods DELETE and so on are inconsistently implemented. If you have a REST api and want a Websockets version it is flat out impossible.

People are saying a single API endpoint which accepts json GET or POST is the future.

I think this is somewhat overblown... You definately can have a REST API run over Websockets, I've implemented many, and they work fine. A GET/POST only API is how many APIs are already implemented.

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