Javascript -- you never need to use "new"

Started by Darren Dirt, December 07, 2012, 09:21:01 AM

Previous topic - Next topic

Darren Dirt

_____________________

Strive for progress. Not perfection.
_____________________

Mr. Analog

... unless you ARE creating an object and you WANT to take advantage of prototype/constructor function

I dislike articles like this because it only tells half the story.
By Grabthar's Hammer

Darren Dirt

#2
Quote from: Mr. Analog on December 07, 2012, 09:31:39 AM
... unless you ARE creating an object and you WANT to take advantage of prototype/constructor function

I dislike articles like this because it only tells half the story.

Well, yeah, kinda*...


keep in mind that the word "new" can be replaced by {} for objects/functions and [] for arrays -- that's the key takeaway I got, that triggering the extra "under the hood" events via "new" is unnecessary and can get in the way of optimal code execution speed.

I found the above via http://stackoverflow.com/questions/907225/object-oriented-javascript-best-practices

so yes obviously there's no such thing as the only "right" way of doing things, but the guy I linked to in the OP definitely has experience with heavy prototype/constructor and object-manipulation stuff. Maybe his focus is on educating the 99% who will continue to do extra/unnecessary/inefficient stuff until someone suggests they don't have to...


heck, here's another voice, this time saying that "this" isn't actually necessary!
http://peter.michaux.ca/articles/javascript-widgets-without-this



*but here's food for thought: "we can eliminate classical inheritance altogether"
http://stackoverflow.com/questions/385403/prototype-based-object-orientation-the-good-the-bad-and-the-ugly


...then again, eventually the options can make your head asplode (cf. http://stackoverflow.com/questions/8242697/javascript-functions-to-simulate-classes-best-practices )
_____________________

Strive for progress. Not perfection.
_____________________

Mr. Analog

I think you missed what I was saying, if you are creating an object and you NEED a CTOR to do something or you want to create a hierarchical object structure (inheritance) you are going to use the "new" keyword. So saying you "never" need it is like saying you'll never need water skis (until you go water skiing that is)

I mean, this isn't a binary thing, using a keyword that adds hints to the JIT compiler is very important for some things and not so much for others. Use as required, use with discretion, learn what it does.

As well I look at the age of this article, so much has changed since then... still coming out of the Dark Ages of script
By Grabthar's Hammer

Darren Dirt

Quote from: Mr. Analog on December 07, 2012, 09:55:33 AM
I think you missed what I was saying, if you are creating an object and you NEED a CTOR to do something or you want to create a hierarchical object structure (inheritance) you are going to use the "new" keyword. So saying you "never" need it is like saying you'll never need water skis (until you go water skiing that is)

I mean, this isn't a binary thing, using a keyword that adds hints to the JIT compiler is very important for some things and not so much for others. Use as required, use with discretion, learn what it does.

As well I look at the age of this article, so much has changed since then... still coming out of the Dark Ages of script

Touche.

it's just that most non-uberadvanced JS programmers use it out of habit when it's not needed, i.e. when they are doing anything OTHER THAN creating a pseudo "instance" of a pseudo "class".

And in fact one of the links above shows some different ways of achieving pretty much the same goal of "inheritance". Without it.
_____________________

Strive for progress. Not perfection.
_____________________

Mr. Analog

I think it's more a case of "monkey-see, monkey-do" where people pick up syntax off the web without actually learning about it and the author of this article is using the wrong approach to teach people discretion.
By Grabthar's Hammer

Thorin

Wait, you're saying I shouldn't write my code just like that monkey's code?  But what if he's the one in a gajillion monkey that actually wrote the code to figure out the Ultimate Question of Life, the Universe, and Everything?
Prayin' for a 20!

gcc thorin.c -pedantic -o Thorin
compile successful

Mr. Analog

Well, if your code looks like this:

/bb|[^b]{2}/

then you've got the wrong monkey (another clue is the sound of infinite typewriters).
By Grabthar's Hammer

Thorin

damn regular expressions, why do they monkeys have to be smart enough to know how to code them?
Prayin' for a 20!

gcc thorin.c -pedantic -o Thorin
compile successful

Mr. Analog

Quote from: Thorin on December 07, 2012, 01:55:38 PM
damn regular expressions, why do they monkeys have to be smart enough to know how to code them?

Well if they didn't who would write Perl? http://instantrimshot.com/
By Grabthar's Hammer

Tom

Quote from: Mr. Analog on December 07, 2012, 02:14:58 PM
Quote from: Thorin on December 07, 2012, 01:55:38 PM
damn regular expressions, why do they monkeys have to be smart enough to know how to code them?

Well if they didn't who would write Perl? http://instantrimshot.com/
OOOoooohhhhh oooohhhhhh AAAAHHHHHH AAAAHHHHHH!!!!! OOOook Ooookkk.
<Zapata Prime> I smell Stanley... And he smells good!!!

Mr. Analog

By Grabthar's Hammer

Thorin

That did not come from a regular expression.

Bu' yeah, I can't code in Monkey yet.  Cow, on the other hand...
Prayin' for a 20!

gcc thorin.c -pedantic -o Thorin
compile successful

Darren Dirt

[slightly on-topic]Timesplitters 4: fanware or never gonna happen ware?[/sot]
_____________________

Strive for progress. Not perfection.
_____________________

Mr. Analog

They are firmly in the depths of the DNF Zone, and lost forever.
By Grabthar's Hammer