JavaScript: Function Invocation Patterns
11–20 of 52 posts
Re: JavaScript: Function Invocation Patterns
#12It 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
#13Should have mentioned call along with apply. Not sure these are patterns rather than just language features.
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.
Re: JavaScript: Function Invocation Patterns
#14 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
#15Or use `bind` instead.
Re: JavaScript: Function Invocation Patterns
#16The 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…
https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...
Re: JavaScript: Function Invocation Patterns
#17One 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
#18please 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..…
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
#19The 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
#20'use strict' makes the this variable undefined in functions invocation.