|
Let me back up a little and make sure I know what you're saying.Subjunctive posted:Function declarations are hoisted, while the assignment of a function expression's result to a variable only occurs when that line of code is reached. Assigning an anonymous expression result requires the developer to think about the ordering of their source in a way that simple declarations don't. By this, are you referring to the fact that JavaScript code:
JavaScript code:
|
# ? May 29, 2015 10:19 |
|
|
# ? Jun 5, 2024 06:36 |
|
http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/ This is a pretty good article on why hoisting does what it does.
|
# ? May 29, 2015 17:28 |
|
I'm a full stack web guy who basically uses JS to glue libraries as much as possible, since I like C# so drat much. I finally get to the point where I do a little JS, and I promptly pick my jaw up off the floor, since you can't overload functions. Now that I've stopped the room from spinning, what's the way in which we null check our arguments? Is it just if (param) or do we go if (param === null) or does it depend on which browser?
|
# ? May 29, 2015 19:37 |
|
Space Whale posted:I'm a full stack web guy who basically uses JS to glue libraries as much as possible, since I like C# so drat much. I finally get to the point where I do a little JS, and I promptly pick my jaw up off the floor, since you can't overload functions. Depends on what you're expecting. Often you're just checking whether things are false-y, so you can just say if (param) {. If you're expecting false-y input, then check for null with ===. You can also do something like JavaScript code:
|
# ? May 29, 2015 19:57 |
|
^^^ that Also if you want to check specifically that something hasn't been defined at all: if(typeof thing == 'undefined')
|
# ? May 29, 2015 20:02 |
|
qntm posted:Let me back up a little and make sure I know what you're saying. Yes, but also that if you have two functions a and b that call each other, then JavaScript code:
|
# ? May 29, 2015 20:36 |
|
E: never mind
qntm fucked around with this message at 01:46 on May 30, 2015 |
# ? May 29, 2015 21:53 |
|
Space Whale posted:you can't overload functions. Yeah, JavaScript is not C# obstipator posted:if(typeof thing == 'undefined') Or just if(thing === undefined) edit: Space Whale posted:if (param === null) or does it depend on which browser? If you're going that route, check against undefined, not null.
|
# ? May 30, 2015 12:27 |
|
Space Whale posted:I'm a full stack web guy who basically uses JS to glue libraries as much as possible, since I like C# so drat much. I finally get to the point where I do a little JS, and I promptly pick my jaw up off the floor, since you can't overload functions. Are you trying to "fake" overloading by checking if certain parameters are null? Because you probably shouldn't do that.
|
# ? May 30, 2015 14:35 |
|
Wheany posted:Or just if(thing === undefined)
|
# ? May 30, 2015 17:52 |
|
HappyHippo posted:Are you trying to "fake" overloading by checking if certain parameters are null? Because you probably shouldn't do that. What I was trying to do was call a resource in AngularJS with no params to just do /foo/api/bar/GET/, and with param it would do /foo/api/bar/GET/?preDefinedParam="baz". What should I be doing?
|
# ? May 30, 2015 18:04 |
|
Space Whale posted:What I was trying to do was call a resource in AngularJS with no params to just do /foo/api/bar/GET/, and with param it would do /foo/api/bar/GET/?preDefinedParam="baz". code:
|
# ? May 30, 2015 20:02 |
|
Space Whale posted:What I was trying to do was call a resource in AngularJS with no params to just do /foo/api/bar/GET/, and with param it would do /foo/api/bar/GET/?preDefinedParam="baz". It's hard to give specific advice without knowing more about what you're doing, but in general you factor out common operations into a separate function and then write two functions to call it: code:
In a more complex scenario you can use a sort parameters object pattern, which works well with javascript's object literals. Basically you make the function so that you call it like this: code:
|
# ? May 31, 2015 16:26 |
|
HappyHippo posted:You're basically forcing anyone using your function to read the implementation in order to understand that they can in fact omit certain parameters all for a pretty dubious benefit, IMO. Look, I know this is JavaScript and we're all agile rockstars/cowboys/ninjas here, but you could also document your interfaces.
|
# ? May 31, 2015 17:32 |
|
Wheany posted:Look, I know this is JavaScript and we're all agile rockstars/cowboys/ninjas here, but you could also document your interfaces. Of course you can and should, but it's always better to reduce the number of places where confusion or inconsistency can seep in. Especially since you don't gain anything by trying to emulate function overloading in a language where it really doesn't work.
|
# ? May 31, 2015 18:48 |
|
The concept of sending an object for arguments can actually be a really good idea if you have a lot of optional parameters to pass along. Ruby programmers tend to rave about this a lot.
|
# ? May 31, 2015 19:48 |
|
Yeah, I'm a big fan of making functions more dynamic by throwing in an "options" object as a parameter. Declaring a dozen mostly-identical permutations of a function can be a headache in its own right, especially when it comes to inventing/remembering names for all of them.
|
# ? May 31, 2015 21:33 |
|
design better apis so that doesn't happen?
|
# ? May 31, 2015 22:17 |
|
Suspicious Dish posted:design better apis so that doesn't happen? You want to elaborate on that at all?
|
# ? May 31, 2015 23:18 |
|
obstipator posted:The concept of sending an object for arguments can actually be a really good idea if you have a lot of optional parameters to pass along. Ruby programmers tend to rave about this a lot. WrathOfBlade posted:You want to elaborate on that at all?
|
# ? Jun 1, 2015 03:13 |
|
HappyHippo posted:Of course you can and should, but it's always better to reduce the number of places where confusion or inconsistency can seep in. Especially since you don't gain anything by trying to emulate function overloading in a language where it really doesn't work. You get an interface that's more idiomatic/familiar to the average JS developer which isn't nothing.
|
# ? Jun 1, 2015 16:50 |
|
WrathOfBlade posted:You want to elaborate on that at all? Give methods that do different things different names, and not just different signatures?
|
# ? Jun 1, 2015 16:52 |
|
if you really need eight dozen permutations of simple parameters, you need to rethink your api surface and what you really want to expose to consumers
|
# ? Jun 1, 2015 17:42 |
|
Munkeymon posted:You get an interface that's more idiomatic/familiar to the average JS developer which isn't nothing. Since when is faking function overloading more idiomatic?
|
# ? Jun 1, 2015 20:15 |
|
HappyHippo posted:Since when is faking function overloading more idiomatic? Since jQuery became popular as far as I can tell. E: come to think of it, several classes and methods in the spec do different things based on what you pass to them as well, so it was pretty well established before jQ. Munkeymon fucked around with this message at 21:39 on Jun 1, 2015 |
# ? Jun 1, 2015 20:47 |
|
Munkeymon posted:Since jQuery became popular as far as I can tell. If you don't count cases where there are simply optional parameters, then there aren't very many that I can think of. One of them is my fault.
|
# ? Jun 2, 2015 00:42 |
|
Subjunctive posted:If you don't count cases where there are simply optional parameters, then there aren't very many that I can think of. One of them is my fault. I'm counting optional parameters in with fake function overloading since that's basically the same thing depending on what other language you're comparing JS to.
|
# ? Jun 2, 2015 06:14 |
|
code:
|
# ? Jun 2, 2015 18:23 |
|
huhu posted:
Lazier than writing it?
|
# ? Jun 2, 2015 18:28 |
|
Subjunctive posted:Lazier than writing it? Is "lazy" not the word for why stuff like i++ exists? I'd prefer not to have to write -1 if possible. I feel like there's a better way to write it.
|
# ? Jun 2, 2015 18:33 |
|
nope, just write -1.
|
# ? Jun 2, 2015 18:38 |
|
huhu posted:Is "lazy" not the word for why stuff like i++ exists? I'd prefer not to have to write -1 if possible. I feel like there's a better way to write it. Sorry, I thought we were in another thread and you were making fun of something in the code, and I didn't understand why. Code looks good, if you have to iterate downward.
|
# ? Jun 2, 2015 18:52 |
|
huhu posted:Is "lazy" not the word for why stuff like i++ exists?
|
# ? Jun 2, 2015 20:42 |
|
ExcessBLarg! posted:"i++" exists because it's a distinct operator (post-increment) and in compiled languages (e.g., C) often translates to an increment instruction in assembly. Modern C compilers should observe that "i+=1" and even "i=i+1" are the same thing and optimize them accordingly, but this wasn't always the case. It exists in JavaScript because of it's prevalence and familiarity to users of C-family/inspired languages. code:
code:
|
# ? Jun 2, 2015 22:48 |
|
huhu posted:Is "lazy" not the word for why stuff like i++ exists? I'd prefer not to have to write -1 if possible. I feel like there's a better way to write it. You want to access the last index of your array, and that number is always 1 less than the length, because the first item has an index of 0. The -1 is always there because it's the way you convert between the two ways of counting There might be something else you can use in JavaScript (some kind of getLastIndex function) but the way you've done it is pretty standard in programming, and it's lightweight too
|
# ? Jun 3, 2015 00:32 |
|
In this case you could write it as a reduce/fold too.JavaScript code:
or copy with slice and then reverse. (Phone posting, code might not be exact.)
|
# ? Jun 3, 2015 01:09 |
|
Clearly this is a scenario for the "goes to" operator:code:
don't actually do this
|
# ? Jun 3, 2015 01:37 |
|
wrong thread
Tad Naff fucked around with this message at 07:26 on Jun 4, 2015 |
# ? Jun 4, 2015 06:30 |
|
Looks like deep paranoia level sanitizing. Isn't Java strongly typed?
|
# ? Jun 4, 2015 06:35 |
|
|
# ? Jun 5, 2024 06:36 |
|
MasterSlowPoke posted:Looks like deep paranoia level sanitizing. Isn't Java strongly typed? Yes. Javascript is not, though. Guess you were referring to the now-removed post above?
|
# ? Jun 4, 2015 18:28 |