News for hackers. Stuff that matters.
Re:How Good C# Habits can Encourage Bad JavaScript Habits by kevingadd on Thursday May 09, 2013 @04:11PM (#5682636)
Part one's claim that using the 'new' keyword is somehow implicitly bad needs more to back it up than Douglas Crockford saying so. Douglas Crockford's opinions are just opinions. Is it slower in certain JS runtimes? Harder to read? Less maintainable?

Anyway, re: part 2

You shouldn't encourage the use of 'value || defaultValue' without clearly, explicitly pointing out that this will prevent the use of falsy values as argument values - including 0, false, and an empty string... all values people actually want to use in some cases. It seems inexcusable to me that you would write:

"Based on the false-y rules we learned earlier, if we want to check if a variable is undefined, not null, and has a value then all we need to do is check the object itself."

Immediately after explaining how null and undefined are not the only falsy values in JS.

Anyway, otherwise it looks like you're sharing some good advice for newbies.

Re:How Good C# Habits can Encourage Bad JavaScript Habits by kbenson on Thursday May 09, 2013 @05:56PM (#5683238)
> You shouldn't encourage the use of 'value || defaultValue' without clearly, explicitly pointing out that this will prevent the use of falsy values as argument values - including 0, false, and an empty string... all values people actually want to use in some cases.

Perl dealt with this a few versions back, and now they have an operator for "defined-or", // [1]. It's useful.

This makes it easier to correct the wrong (but common) way to supply default values for undefined/unpassed values:

    # Old wrong way
    my $foo = $bar || $baz; # $this will never be used if "falsy": 0, '', undef, '0E0'

    # Old right ways
    my $foo = defined $bar ? $bar : $baz;
    # Or
    my $foo = $bar;
    $foo = $baz unless defined $foo;
    
    # New way
    my $foo = $bar // $baz; # Uses $baz only if not defined $bar


 [1]: http://perldoc.perl.org/perlop.html#Logical-Defined-Or
Re:How Good C# Habits can Encourage Bad JavaScript Habits by anonymfus on Thursday May 09, 2013 @06:57PM (#5683511)
Re:How Good C# Habits can Encourage Bad JavaScript Habits by goldfeld on Thursday May 09, 2013 @07:19PM (#5683587)
From my understanding CoffeeScript's '?' operator does exactly this, as in foo = bar ? baz
Re:How Good C# Habits can Encourage Bad JavaScript Habits by k3n on Thursday May 09, 2013 @05:40PM (#5683142)
There is nothing wrong with `new` at all, and abiding to something just because some other [fallible] human says so couldn't be more wrong.
Re:How Good C# Habits can Encourage Bad JavaScript Habits by jiggy2011 on Thursday May 09, 2013 @06:21PM (#5683380)
The problem is if you forget it.

  x = new ConstructorFunction()
vs

  x = ConstructorFunction()
Both will work without raising an error but the second will potentially write stuff into the global object, leading to some weird bugs.
Re:How Good C# Habits can Encourage Bad JavaScript Habits by tracker1 on Thursday May 09, 2013 @08:25PM (#5683808)
function ConstructorFunction() {

  if (!(this instanceof ConstructorFunction)) 

    throw new Error("new keyword required");

  ...

}
Re:How Good C# Habits can Encourage Bad JavaScript Habits by k3n on Thursday May 09, 2013 @08:51PM (#5683890)
Yes, the Crockford argument. Just because he said it doesn't make it true, though.

I don't forget it, because calling a function and creating a new object are 2 totally different things. If one can't distinguish between those, then they should probably be tutored in programming 101 because you're going to end up with a lot more serious problems than a ctor accidentally called as a normal function.

Also, and I'm going out on a limb here, but I'd hazard a guess that a majority of programmers using JS will have experience with at least 1 other language (if not many) which use the very same paradigm of creating objects. I can think of a handful off the top of my head, with several of them having a virtually identical syntax (C#, Java, PHP).

Re:How Good C# Habits can Encourage Bad JavaScript Habits by jiggy2011 on Thursday May 09, 2013 @09:09PM (#5683960)
None of those languages AFAIK (I don't really know C#) will allow you to call a constructor function as anything except a constructor function and constructor functions are always constructors and therefor will always return a new object.

Javascript in it's infinite wisdom will allow one to use any function as a constructor or not. So code the should be obviously wrong will not generate an error or even a warning, this is not something you want from a language. Since we can't change JS , there is merit to the argument that you should change your own code to avoid even possibly having this problem.

Arguments made on the basis of "this is a mistake I am unlikely to make" fill me with skepticism.

Re:How Good C# Habits can Encourage Bad JavaScript Habits by k3n on Thursday May 09, 2013 @10:19PM (#5684161)
You can shoot yourself in the foot in any language....have you ever tried Perl?

Also, if you name your objects and functions like most anyone in CS would suggest, e.g. objects are nouns and functions are verbs, there really shouldn't be a disconnect between the 2 when you need to call them. I am confident that is not a mistake I'll tend to make.

Re:How Good C# Habits can Encourage Bad JavaScript Habits by jiggy2011 on Thursday May 09, 2013 @11:17PM (#5684384)
I would suggest movement in the direction of less foot shooting.
Re:How Good C# Habits can Encourage Bad JavaScript Habits by e12e on Thursday May 09, 2013 @09:47PM (#5684076)
Regarding the use of the "new" keyword, I found this stackoverflow question and answer illuminating:

http://stackoverflow.com/questions/1646698/what-is-the-new-k...

Re:How Good C# Habits can Encourage Bad JavaScript Habits by cmwelsh on Thursday May 09, 2013 @06:18PM (#5683366)
I think the Backbone.js-style class pattern is something that helps keep your code modular and well organized. Prototype are nothing to fear.

However, the article does not really touch on this. It's mostly just saying to use `{}` instead of `new Object`.

[deleted]
Re:How Good C# Habits can Encourage Bad JavaScript Habits by k3n on Thursday May 09, 2013 @05:44PM (#5683166)
You're misinterpreting the results.

> operations per second (higher is better)

Using `new` is in red, while using `Object.create` is in blue, and the red bars are markedly larger than the blue in all but a few cases.

Thus, using `new` is, for the most part, much faster than the Crockford way. In Chrome, it's tons faster.

Re:How Good C# Habits can Encourage Bad JavaScript Habits by stoey on Thursday May 09, 2013 @05:56PM (#5683236)
That's not actually what the Crockford link says [1] it talks about using array and object literals in place of `new Object()` or `new Array()`. Which does indeed seem faster [2].

[1] http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-... [2] http://jsperf.com/new-vs-object-literal