Live data from Hacker News

JavaScript: Function Invocation Patterns

doctrina.org

11–20 of 52 posts

Re: JavaScript: Function Invocation Patterns

#12
One portion of Douglas Crockford's book "JavaScript: The Good Parts" describes invocation patterns thoroughly.

It might be that the author of the post was inspired by this book. Since he hasn't mentioned it, I did it in case someone wants to know more about the topic (and doesn't know about the book).

Re: JavaScript: Function Invocation Patterns

#13
post #3

Should have mentioned call along with apply. Not sure these are patterns rather than just language features.

Exactly. Stuff that's built directly into the language isn't a pattern, it's a feature (perhaps a wart in this case). From Wikipedia [1]:

In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations. Patterns are formalized best practices that the programmer must implement themselves in the application.

[1] http://en.wikipedia.org/wiki/Software_design_pattern

Re: JavaScript: Function Invocation Patterns

#14
The big bug as I see it is:

   obj = {
     get_name = function() { return "I am an object" },
     func = function() { alert(this.get_name()); }
   }
This looks dandy, and obj.func() works as expected.

but if you pass obj.func as a callback, the this when its invoked will be some other object (by default, the window object):

   button.onclick = obj.func; // bang when invoked
The number of times I got screwed by that. You end up having to have little anonymous functions for all callbacks:

    button.onclick = function(evt) { obj.func(); }
(apologies for bugs; just typing javascript from memory)

(would love to be wrong)

Re: JavaScript: Function Invocation Patterns

#16

The big bug as I see it is: obj = { get_name = function() { return "I am an object" }, func = function() { alert(this.get_name()); } } This looks dandy, and obj.func() works as expected. but if you pass obj.func as a callback, the this when its invoked will be some other object (by default, the window object): button.onclick = obj.func; // bang when invoked The number of times I got screwed by that. You end up having…

bind was introduced in ES5 (and previously in most libraries) to fix this

https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...

Re: JavaScript: Function Invocation Patterns

#17
post #12

One portion of Douglas Crockford's book "JavaScript: The Good Parts" describes invocation patterns thoroughly. It might be that the author of the post was inspired by this book. Since he hasn't mentioned it, I did it in case someone wants to know more about the topic (and doesn't know about the book).

He does actually refer to Crockfords book roughly half way through.

Re: JavaScript: Function Invocation Patterns

#18
post #5

please see the HN guidelines http://ycombinator.com/newsguidelines.html Please don't do things to make titles stand out, like using uppercase or exclamation points, or adding a parenthetical remark saying how great an article is. It's implicit in submitting something that you think it's important. plus i have a very strong distain about anyone or anything (i.e. headlines) which tell me what i MUST do / know / think..…

> the article above is about a very basic language feature of JS that hopefully anyone who has every touched JS already knows about.

Hopefully, yes, but I think reality is much more depressing than that. Unfortunately, the JavaScript programmers that don't know probably don't read Hacker News either.

Re: JavaScript: Function Invocation Patterns

#19
post #16

The big bug as I see it is: obj = { get_name = function() { return "I am an object" }, func = function() { alert(this.get_name()); } } This looks dandy, and obj.func() works as expected. but if you pass obj.func as a callback, the this when its invoked will be some other object (by default, the window object): button.onclick = obj.func; // bang when invoked The number of times I got screwed by that. You end up having…

bind was introduced in ES5 (and previously in most libraries) to fix this https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...

And trivially easy to shim for older browsers. I would really recommend using Function.prototype.bind instead of the 'var self = this' or library bind functions (such as $.proxy), it's just the way it should be written :)
Post reply on HN