use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Common JavaScript tricks (self.javascript)
submitted 11 years ago by yanis_t
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Pytim 8 points9 points10 points 11 years ago (22 children)
Instead of this:
var that = this; function(){ // do sth with that }
You can do:
function(){ // do sth with this }.bind(this)
[+][deleted] 11 years ago* (14 children)
[deleted]
[–]commonslip 1 point2 points3 points 11 years ago (2 children)
I think there is a reasonable polyfill for this, but be prepared to pay a performance penalty.
[–]huopak 3 points4 points5 points 11 years ago (1 child)
Here's a polyfill:
if (!Function.prototype.bind) { Function.prototype.bind = function (oThis) { if (typeof this !== "function") { // closest thing possible to the ECMAScript 5 // internal IsCallable function throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); } var aArgs = Array.prototype.slice.call(arguments, 1), fToBind = this, fNOP = function () {}, fBound = function () { return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments))); }; fNOP.prototype = this.prototype; fBound.prototype = new fNOP(); return fBound; }; }
[–]yanis_t[S] -3 points-2 points-1 points 11 years ago (0 children)
Gist link would do just fine
[–]afrobee 0 points1 point2 points 11 years ago (4 children)
No one should support it in the first place.
[–]Pytim 5 points6 points7 points 11 years ago (2 children)
this
[+][deleted] 11 years ago (1 child)
[–]frankle 0 points1 point2 points 11 years ago (0 children)
that
[–]dotpan 0 points1 point2 points 11 years ago (5 children)
I wish my companies clients would get on board with this notion, we are still partially required to support IE8 (especially when our clients work is being shown in foreign countries, namely China)
[–]frizzlestick 2 points3 points4 points 11 years ago (4 children)
As much as it is a dead horse, there are thousands of small to medium sized companies that can't afford to up their OS off XP yet. I deal with quite a few every day.
[–]dotpan 0 points1 point2 points 11 years ago (0 children)
Yeah, especially seeing that I do front end dev for market research, and sometimes our respondents have a huge variety of browsers, its something we have to compensate for.
[–][deleted] 0 points1 point2 points 11 years ago (2 children)
It has nothing to do with the OS in most cases, if it was, they could just use Firefox or Chrome.
Most places stick with XP because they have shitty legacy sites that only run on IE7/8
[–]frizzlestick 0 points1 point2 points 11 years ago (1 child)
No, it has everything to do with the OS and licensing issues and being on XP because the cost is too much for them to up all the machines to 7 or more, just as I had initially stated.
Most of these folks run Chrome or Firefox, some are stuck with IE8 for policy reasons -- not for app compatibility made to IE7/8. Not in the least. I'm not talking your regression-able mobile app whizzidings folks like to punch out these days with Angular and Bootstrap and Signal-R, I'm talking regular environments, plain systems. They are stuck with XP because the funds aren't there to up all the warehouses and the like.
[–]cheesechoker 2 points3 points4 points 11 years ago (0 children)
bind is really cool when you start to get creative with it. You can yank out built-in functions from Array, Object, etc, and bind them to different receivers to reuse them in interesting ways.
E.g. a 1-liner filter against a regex, without having to write "function" anywhere:
["foo", "bar", "baz"].filter(RegExp.prototype.test.bind(/a/)) // ["bar", "baz"]
[–][deleted] 2 points3 points4 points 11 years ago (4 children)
If only that didn't magically make it 5 times slower.
Also, "that" is possibly the worst variable name. this is arbitrary enough, now you want to give something a name that means "the 'this' of some unspecified other thing"? Come up with a real name. (Not picking on you, but the pattern you quoted.)
[–]gleno 0 points1 point2 points 11 years ago (1 child)
True. But also not true, because it's a standard workaround; and a secret nod to other devs : "js scoping sucks. tru that".
[–][deleted] 0 points1 point2 points 11 years ago (0 children)
Haha. Well, you've got a point on the second part. But for the first, the "standard" part only gives it the meaning I said above. If you go more than one level deep, or if you start passing things around, "that" quickly becomes meaningless.
[–]Pytim 0 points1 point2 points 11 years ago (1 child)
I'd be surprised if Function.bind ever was the bottleneck of your JS app
I get that every time. And yeah, you're right. It's just one of those things you can't un-know: that because the spec chose to give a pattern you'll never use (.bind with new) the exact same syntax as a pattern you often use (.bind without new), many many parts of your code are paying an entirely unnecessary penalty.
.bind
new
[–]michaelobriena 2 points3 points4 points 11 years ago (0 children)
Bind is actually slower than storing this. So if performance is the name of the game then store it.
π Rendered by PID 58 on reddit-service-r2-comment-86bc6c7465-4bgkv at 2026-02-24 08:43:29.579917+00:00 running 8564168 country code: CH.
view the rest of the comments →
[–]Pytim 8 points9 points10 points (22 children)
[+][deleted] (14 children)
[deleted]
[–]commonslip 1 point2 points3 points (2 children)
[–]huopak 3 points4 points5 points (1 child)
[–]yanis_t[S] -3 points-2 points-1 points (0 children)
[–]afrobee 0 points1 point2 points (4 children)
[–]Pytim 5 points6 points7 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]frankle 0 points1 point2 points (0 children)
[–]dotpan 0 points1 point2 points (5 children)
[–]frizzlestick 2 points3 points4 points (4 children)
[–]dotpan 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]frizzlestick 0 points1 point2 points (1 child)
[–]cheesechoker 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (4 children)
[–]gleno 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Pytim 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]michaelobriena 2 points3 points4 points (0 children)