Live data from Hacker News

JavaScript: Function Invocation Patterns

doctrina.org

31–40 of 52 posts

Re: JavaScript: Function Invocation Patterns

#31
Hi Guys

I just want to give you some feedbacks in the comments (I am the author of this article).

Firstly, with regards to the title, I did not read the newsguidlines document - I have submitted links in the way and style that other people did. For sure, in the future, I will follow these guidlines, but this must be a huge problem as lots of people do this.

Secondly, these are patterns (not language features). Yes, I was inspired by Douglas Crockford's book where he describes this as patterns. To me describing them as patterns makes sense: For instance, there is really nothing different about calling a method and a function. If you have to make a difference, then it is a pattern. And because putting new in front of a function also calls that function, it is in my mind a pattern. And in that sense, so is apply.

But I welcome the debate, and thanks for the constructive comments. I will take all these comments into consideration the next time I submit something.

Re: JavaScript: Function Invocation Patterns

#32
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..…

Amusingly, the original title has a spelling mistake: "Javascript: Function Inovcation Patterns." But enough burying the lede. The issue I have with this title is simple: It's not clear WHY I must know these patterns: The focus of the post is not why I must know them, but rather This post explains the four patterns, how to use them and what to watch out for. Had the post kicked off by explaining that not knowing one…

It still does: the language is "JavaScript", not "Javascript".

Re: JavaScript: Function Invocation Patterns

#33
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..…

How about instead of hijacking the thread to bitch about guidelines 1% of the HN population knows about you let the down arrow do your talking?

Re: JavaScript: Function Invocation Patterns

#34

Earlier quoted context omitted.

Amusingly, the original title has a spelling mistake: "Javascript: Function Inovcation Patterns." But enough burying the lede. The issue I have with this title is simple: It's not clear WHY I must know these patterns: The focus of the post is not why I must know them, but rather This post explains the four patterns, how to use them and what to watch out for. Had the post kicked off by explaining that not knowing one…

It still does: the language is "JavaScript", not "Javascript".

While we are at nitpicking: I think this is really about ECMAScript.

Re: JavaScript: Function Invocation Patterns

#35

He'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…

I realise that everyone is critic, but I am sorry: I understand fully how `this` gets bound and how scoping works in Javasscript. And it sucks!

Scoping: Javascript uses C/Java notation but it does not have block scope, only function scope. That to me seems very misleading.

And with regards to `this`, the way function invocation sets `this` differs totally from method invocation. How can this not be a problem!

Re: JavaScript: Function Invocation Patterns

#36
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..…

How about instead of hijacking the thread to bitch about guidelines 1% of the HN population knows about you let the down arrow do your talking?

Because it helps educate the 99% who might otherwise think this is an acceptable title format.

Re: JavaScript: Function Invocation Patterns

#37

He'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…

I realise that everyone is critic, but I am sorry: I understand fully how `this` gets bound and how scoping works in Javasscript. And it sucks! Scoping: Javascript uses C/Java notation but it does not have block scope, only function scope. That to me seems very misleading. And with regards to `this`, the way function invocation sets `this` differs totally from method invocation. How can this not be a problem!

> Scoping: Javascript uses C/Java notation but it does not have block scope, only function scope. That to me seems very misleading.

Yes, it is, but we weren't talking about that problem. That is already solved in ES6 with `let`, which is approved and will be available in about a year.

> And with regards to `this`, the way function invocation sets `this` differs totally from method invocation. How can this not be a problem!

Function and method invocation are different things, with different semantics. It's like that in many other languages too. Sure, the way `this` is handles is a problem sometimes, but I think it's overblown.

Re: JavaScript: Function Invocation Patterns

#39
post #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.

Actually, I only have a vague understanding of this and I'm probably what you would call an expert. I have used many of these features in JS, but like the author says it's unintuitive and "wrong" the way javascript does it. Basically OOP is a horrible hack in JS, and this is a very good article explaining various gotchas that I wasn't aware of.

Re: JavaScript: Function Invocation Patterns

#40
post #24

Earlier quoted context omitted.

Nope, he doesn't. :)

He does, but it's indirect. In the article he says: "There is a remedy which was championed by Douglas Crockford: Augment Object with a create method that accomplishes what the constructor invocation pattern tries to do." 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 ur…

Thanks for pointing it out. I didn't see it. Your comment motivated me to look at the whole post about JavaScript prototypes again and read it. It is very good, I refreshed my knowledge about Object.prototype, Function.prototype design.
Post reply on HN