|
Hey dudes, another Q. Can you confirm that there's no way to overload operators in JS? this post seems pretty clean cut, but I'm wondering if there are any tricks. Ie, for the datetime library I'm working on, I'd like to define behaviour to add or subtract timedeltas. I could do it with syntax like this:JavaScript code:
JavaScript code:
|
# ? May 29, 2017 14:43 |
|
|
# ? May 15, 2024 05:28 |
|
Dominoes posted:Hey dudes, another Q. Can you confirm that there's no way to overload operators in JS? this post seems pretty clean cut, but I'm wondering if there are any tricks. Ie, for the datetime library I'm working on, I'd like to define behaviour to add or subtract timedeltas. I could do it with syntax like this: Operator overloading is bad. Use a function.
|
# ? May 29, 2017 17:55 |
|
Dominoes posted:Hey dudes, another Q. Can you confirm that there's no way to overload operators in JS? this post seems pretty clean cut, but I'm wondering if there are any tricks. Ie, for the datetime library I'm working on, I'd like to define behaviour to add or subtract timedeltas. I could do it with syntax like this: Do it like this: JavaScript code:
Dominoes posted:I think I can't do the kwarg style either, so the delta would have to be TimeDelta(0, 3). Is that right? See above. You can use an object for parameters, and if you're using ES6 or Typescript, automatically destructure it in the function definition: JavaScript code:
code:
|
# ? May 29, 2017 19:35 |
|
Thanks dude! I'm going for funcs over methods when able for style reasons. That destructuring syntax in place of kwargs looks perfect. Didn't know about multiple constructors either.
|
# ? May 29, 2017 19:53 |
|
Dominoes posted:Thanks dude! I'm going for funcs over methods when able for style reasons. Don't. Everyone will hate you for doing it (and won't use the library), because chained methods is the style that literally everyone else uses. Edit: When it doubt, use the general style of Immutable.js as a pattern to follow for data-manipulation libraries. Dominoes posted:Didn't know about multiple constructors either. In Typescript, multiple method definitions is basically just sugar over whatever the last method definition is, in that it'll map your typings to whichever is appropriate for documentation/display purposes, but the last one needs to accommodate all of them at once. Roadie fucked around with this message at 01:17 on May 30, 2017 |
# ? May 30, 2017 01:07 |
|
Random question, on these two case statements:code:
|
# ? Jun 1, 2017 11:50 |
|
Honest Thief posted:Random question, on these two case statements: I don't think a closure is created for either. Closures do not map to { }, a closure is not created for if (true) { }. Closures are created around functions.
|
# ? Jun 1, 2017 16:54 |
|
Honest Thief posted:Random question, on these two case statements: In a confusingly related concept, case b will create a new scope for ES6 block-scoped variables (const and let) while a won't, since the {} will create a new block. code:
zombienietzsche fucked around with this message at 17:51 on Jun 1, 2017 |
# ? Jun 1, 2017 17:06 |
|
meinstein posted:In a confusingly related concept, case b will create a new scope for ES6 block-scoped variables (const and let) while a won't, since the {} will create a new block. This is true, but that is not the same thing as a closure. Upon closer reading your comment, you do mention that.
|
# ? Jun 1, 2017 17:19 |
|
There are no closures in either case. The brackets for case b only affect scope for variables defined with let. edit: shoulda reloaded.
|
# ? Jun 1, 2017 17:47 |
|
Ah right, I keep conflating closure with scope a lot. I should keep a reminder close so as to not confuse the two.
|
# ? Jun 2, 2017 13:36 |
|
Stupid class question: this highlighted variable is undefined for some reason? To quote a similar usage example from the MDN's classes documentation: php:<? class Rectangle { constructor(height, width) { this.height = height; this.width = width; } ... calcArea() { return this.height * this.width; } }?>
|
# ? Jun 4, 2017 12:54 |
|
Love Stole the Day posted:
Yes, remove const.
|
# ? Jun 4, 2017 14:45 |
|
Yup, properties cannot be constant. If you need something that is only readable you need to define a property with a getter and no setter. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
|
# ? Jun 4, 2017 17:18 |
|
I'm finally getting around to making a serious effort at using and learning TypeScript on a project... Am I doing this right? JavaScript code:
|
# ? Jun 4, 2017 20:21 |
|
Thermopyle posted:I'm finally getting around to making a serious effort at using and learning TypeScript on a project... That is an appropriate way to use Generics in TypeScript. Now, you may have trouble calling it if the objects you pass in do not explicitly match the { [key: string] : T } type, but you can also simply not fill in the <T> part when calling it and will set T to any.
|
# ? Jun 4, 2017 22:18 |
|
Skandranon posted:That is an appropriate way to use Generics in TypeScript. Now, you may have trouble calling it if the objects you pass in do not explicitly match the { [key: string] : T } type, but you can also simply not fill in the <T> part when calling it and will set T to any. Thanks. I think that objects can only have strings as keys, no? (Well also Symbols, but I don't care about them.)
|
# ? Jun 4, 2017 23:20 |
|
Thermopyle posted:Thanks. I think that objects can only have strings as keys, no? (Well also Symbols, but I don't care about them.)
|
# ? Jun 4, 2017 23:27 |
|
Dominoes posted:Correct. Maps are more flexible. With worse syntax!
|
# ? Jun 4, 2017 23:29 |
|
Agree; I avoid them for that reason.
|
# ? Jun 4, 2017 23:52 |
|
I have two check boxes that toggle the hidden class for a cell's row in a table based on whether the cell's value is "Incomplete" or "Complete. Is there an easy way to combine the following code because almost all of it is repetitive?code:
|
# ? Jun 5, 2017 16:19 |
|
Honest Thief posted:Ah right, I keep conflating closure with scope a lot. I should keep a reminder close so as to not confuse the two. (AFAIK, getting really picky about what a closure *is*,) A local scope is a closure that you can't pass on to any other code as you could a function, so you're not as wrong as you seem to think. Just forget let exists and you're good to go
|
# ? Jun 5, 2017 16:32 |
|
You can refactor the common code out into a separate function. Also, while it shouldn't make a difference in this case, I'd recommend forming a habit to use === instead of == for equality checks.code:
|
# ? Jun 5, 2017 17:16 |
|
Does anyone have any recommendations for books/online courses/etc for more advanced JavaScript concepts? I'm pretty confident with most of the syntax, and can accomplish most things with it, but I'm not really sure how "professional" JavaScript is written. I feel like all the code I write no matter what would make anyone who knows what they are doing slap me in the face.
|
# ? Jun 7, 2017 20:01 |
|
Edit: A large amount of "professional" JavaScript is written by people that don't know what they're doing, and are flying by the seat of their pants cobbling together half-understood examples from Stack Overflow. If you read those two you'll be streets ahead. zombienietzsche fucked around with this message at 20:26 on Jun 7, 2017 |
# ? Jun 7, 2017 20:24 |
|
ddiddles posted:Does anyone have any recommendations for books/online courses/etc for more advanced JavaScript concepts? http://wesbos.com/courses/ has some modern JS courses. His presentation style is a little hit-or-miss for me, but the content's good for the price.
|
# ? Jun 7, 2017 21:04 |
|
ddiddles posted:Does anyone have any recommendations for books/online courses/etc for more advanced JavaScript concepts? "You Don't Know Java script" is a free book on github.com that's really good and free. If you want to know how your professional java script should look, look at the airbnb and Google style guides.
|
# ? Jun 7, 2017 22:15 |
|
meinstein posted:Edit: A large amount of "professional" JavaScript is written by people that don't know what they're doing, and are flying by the seat of their pants cobbling together half-understood examples from Stack Overflow. If you read those two you'll be streets ahead. I've been skimming around on debounce and throttle functions and there are many articles on the topic. Common theme was not to implement your own because libraries are awesome and peer reviewed by Trump himself and so they must be great. So I picked up Lodash and tried their throttle() API and guess what, it is fundamentally flawed and broken, spent this morning rewriting it to be less brain dead but still shines of authors trying too hard for a relatively simple requirement. quote:Creates a throttled function that only invokes func at most once per every wait milliseconds. So the authors decide to implement the rate limit tracking on the incoming calls to the throttle rather than the actual function that is supposed to be throttled. It is just so weird when you attach it to something graphical.
|
# ? Jun 8, 2017 00:38 |
|
I'm not sure I understand the objection. How are you using it? What are you expecting to happen, and how is this different from what it actually does?
|
# ? Jun 8, 2017 00:57 |
|
MrMoo posted:So I picked up Lodash and tried their throttle() API and guess what, it is fundamentally flawed and broken, spent this morning rewriting it to be less brain dead but still shines of authors trying too hard for a relatively simple requirement. lol?
|
# ? Jun 8, 2017 16:17 |
|
Jabor posted:I'm not sure I understand the objection. How are you using it? What are you expecting to happen, and how is this different from what it actually does? https://github.com/lodash/lodash/blob/4.17.4/lodash.js#L10911 they just call debounce, which is... sure an interesting implementation decision because I'd expect them to do very different things. I'd expect throttle to queue up calls for later and debounce to throw away calls from before the timeout expired. Sure, you could handle both of those things with one implementation and some options, but I wouldn't expect throttle to be a special case of debounce.
|
# ? Jun 8, 2017 16:46 |
|
MrMoo posted:So I picked up Lodash and tried their throttle() API and guess what, it is fundamentally flawed and broken, spent this morning rewriting it to be less brain dead but still shines of authors trying too hard for a relatively simple requirement.
|
# ? Jun 8, 2017 16:56 |
|
meinstein posted:
|
# ? Jun 8, 2017 17:30 |
|
Jabor posted:I'm not sure I understand the objection. How are you using it? What are you expecting to happen, and how is this different from what it actually does? Looks like it has been logged before, they are impressively zealous on closing issues in their tracker: https://github.com/lodash/lodash/issues/3051 I used it to throttle updates to a Polymer field which is tied to a display item on the page. The Lodash implementation causes the item to randomly double update. This is my version: MrMoo fucked around with this message at 18:01 on Jun 8, 2017 |
# ? Jun 8, 2017 17:36 |
|
MrMoo posted:Looks like it has been logged before, they are impressively zealous on closing issues in their tracker: They closed it and linked the PR, which then got closed after a week of no response from the PR author. https://github.com/lodash/lodash/pull/3053 With a project as popular as lodash you need to be pretty aggressive with issues/PRs or you end up with hundreds or thousands that sit forever.
|
# ? Jun 8, 2017 17:40 |
|
necrotic posted:They closed it and linked the PR, which then got closed after a week of no response from the PR author. This is exactly right, and I nominate MrMoo to submit his fixed version.
|
# ? Jun 8, 2017 18:23 |
|
"Professional integrator" version, i.e. I copied the debounce() implementation and replaced all Lodash functions with standard ECMA functions where I could be bothered (no exception on function parameter not being a function) and fixed it by using lastInvokeTime instead of lastCallTime for rate checks, everything else as ugly as it was. http://ahyoomee.miru.hk/donald/throttle/throttle.js
|
# ? Jun 8, 2017 19:12 |
|
lodash would take a fix (as indicated by the issue/PR threads) so why not fix it there? We don't need a trillionth throttle package. Also since you copied from lodash maybe give them credit. Or just steal code, that's cool too.
|
# ? Jun 8, 2017 19:50 |
|
necrotic posted:Or just steal code, that's cool too.
|
# ? Jun 8, 2017 20:20 |
|
|
# ? May 15, 2024 05:28 |
|
I think they want a PR and a contributor agreement, I haven't decided what to do with it yet, it's just in a branch for testing a specific data flag. It's F/OSS so good luck stealing it.
|
# ? Jun 8, 2017 20:21 |