JavaScript: Function Invocation Patterns
21–30 of 52 posts
Re: JavaScript: Function Invocation Patterns
#22There is an easy way to get round this problem, but it is in my opinion a hack. One gets around this problem by assigning a varialbe (by convention, it is named that) to this inside the function Or use `bind` instead.
Re: JavaScript: Function Invocation Patterns
#23The 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…
tldr: JavaScript has functions, not methods :)
Re: JavaScript: Function Invocation Patterns
#24One 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
#25Earlier quoted context omitted.
He does actually refer to Crockfords book roughly half way through.
Nope, he doesn't. :)
That sentence contains a link to another page on OP's site which at the top says "Douglas Crockford's wonderful book JavaScript: The Good Parts does a fanastic job of explaining this topic, and I urge the interested reader to buy his book."
Re: JavaScript: Function Invocation Patterns
#26function Foo(type) { this.type = type; return type; }
var bar = new Foo(5);
`bar` DOES equal 5. When you return something in a constructor, it returns it instead of returning the newly constructed object. This is a neat way for constructing different types of things.
And most of his complaints are just because he doesn't understand how `this` scoping works in javascript. If you don't understand something, don't just blame it on "bad patterns". It might not be the most intuitive, but once you learn it, it's fine. Sure, I have to bind `this` to a different function here and there, but these problems are not that bad in real world javascript.
Re: JavaScript: Function Invocation Patterns
#27Re: JavaScript: Function Invocation Patterns
#28He's wrong in the "Constructor Invocation" example. function Foo(type) { this.type = type; return type; } var bar = new Foo(5); `bar` DOES equal 5. When you return something in a constructor, it returns it instead of returning the newly constructed object. This is a neat way for constructing different types of things. And most of his complaints are just because he doesn't understand how `this` scoping works in javasc…
No, it doesn't. Javascript is a bit weird when it comes to return statements inside constructors. `bar` will be equal to `type` only if `(type instanceof Object) == true`. Otherwise, it will be a new object.
function Foo(type) { this.type = type; return type; }
console.log(new Foo(/a/)); // Regexp /a/
console.log(new Foo("a")); // {type: "a"}
console.log(new Foo(5)); // {type: 5}
console.log(new Foo([1,2])); // [1,2]
console.log(new Foo({a:1})); // {a: 1}Re: JavaScript: Function Invocation Patterns
#29He's wrong in the "Constructor Invocation" example. function Foo(type) { this.type = type; return type; } var bar = new Foo(5); `bar` DOES equal 5. When you return something in a constructor, it returns it instead of returning the newly constructed object. This is a neat way for constructing different types of things. And most of his complaints are just because he doesn't understand how `this` scoping works in javasc…
> `bar` DOES equal 5 No, it doesn't. Javascript is a bit weird when it comes to return statements inside constructors. `bar` will be equal to `type` only if `(type instanceof Object) == true`. Otherwise, it will be a new object. function Foo(type) { this.type = type; return type; } console.log(new Foo(/a/)); // Regexp /a/ console.log(new Foo("a")); // {type: "a"} console.log(new Foo(5)); // {type: 5} console.log(new…
Almost all cases that I've used this functionality have been to return a different object. I suppose when you say "new" you're supposed to expect an object, which is why it works that way.
Re: JavaScript: Function Invocation Patterns
#30He's wrong in the "Constructor Invocation" example. function Foo(type) { this.type = type; return type; } var bar = new Foo(5); `bar` DOES equal 5. When you return something in a constructor, it returns it instead of returning the newly constructed object. This is a neat way for constructing different types of things. And most of his complaints are just because he doesn't understand how `this` scoping works in javasc…
> `bar` DOES equal 5 No, it doesn't. Javascript is a bit weird when it comes to return statements inside constructors. `bar` will be equal to `type` only if `(type instanceof Object) == true`. Otherwise, it will be a new object. function Foo(type) { this.type = type; return type; } console.log(new Foo(/a/)); // Regexp /a/ console.log(new Foo("a")); // {type: "a"} console.log(new Foo(5)); // {type: 5} console.log(new…