Righteous Wrath Online Community

General => Tech Chat => Topic started by: Darren Dirt on December 07, 2012, 09:21:01 AM

Title: Javascript -- you never need to use "new"
Post by: Darren Dirt on December 07, 2012, 09:21:01 AM
*NEVER*
http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/



(this guy is smart! http://javascript.crockford.com/ -- and he acknowledges that even he is still learning/improving as time goes by (http://javascript.crockford.com/prototypal.html))
Title: Re: Javascript -- you never need to use "new"
Post by: 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.
Title: Re: Javascript -- you never need to use "new"
Post by: Darren Dirt on December 07, 2012, 09:42:18 AM
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 (http://yuiblog.com/blog/2006/09/26/for-in-intrigue/). 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 )
Title: Re: Javascript -- you never need to use "new"
Post by: 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
Title: Re: Javascript -- you never need to use "new"
Post by: Darren Dirt on December 07, 2012, 10:09:44 AM
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.
Title: Re: Javascript -- you never need to use "new"
Post by: Mr. Analog on December 07, 2012, 10:15:56 AM
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.
Title: Re: Javascript -- you never need to use "new"
Post by: Thorin on December 07, 2012, 12:07:51 PM
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?
Title: Re: Javascript -- you never need to use "new"
Post by: Mr. Analog on December 07, 2012, 12:29:45 PM
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).
Title: Re: Javascript -- you never need to use "new"
Post by: 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?
Title: Re: Javascript -- you never need to use "new"
Post by: 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/
Title: Re: Javascript -- you never need to use "new"
Post by: Tom on December 07, 2012, 02:42:59 PM
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.
Title: Re: Javascript -- you never need to use "new"
Post by: Mr. Analog on December 07, 2012, 03:02:18 PM
ooh AHH-AHH!!!

(http://i.imgur.com/GFSTU.png)
Title: Re: Javascript -- you never need to use "new"
Post by: Thorin on December 07, 2012, 03:37:05 PM
That did not come from a regular expression.

Bu' yeah, I can't code in Monkey yet.  Cow, on the other hand...
Title: Re: Javascript -- you never need to use "new"
Post by: Darren Dirt on December 07, 2012, 03:40:22 PM
[slightly on-topic]Timesplitters 4 (http://en.wikipedia.org/wiki/TimeSplitters_4): fanware or never gonna happen ware?[/sot]
Title: Re: Javascript -- you never need to use "new"
Post by: Mr. Analog on December 07, 2012, 03:49:46 PM
They are firmly in the depths of the DNF Zone, and lost forever.
Title: Re: Javascript -- you never need to use "new"
Post by: Darren Dirt on December 07, 2012, 04:30:05 PM
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/

didja know there's optional parameters?
http://instantrimshot.com/index.php?sound=csi
Title: Re: Javascript -- you never need to use "new"
Post by: Mr. Analog on December 07, 2012, 04:56:41 PM
OH GOD slidewhistle
Title: Re: Javascript -- you never need to use "new"
Post by: Darren Dirt on December 08, 2012, 09:21:22 AM
Quote from: Mr. Analog on December 07, 2012, 04:56:41 PM
OH GOD slidewhistle

there's even a handful of now-hidden ones ... ooo what a mystery...

<a href="http://instantrimshot.com/index.php?sound=bomb">bomb</a>
<!--<a href="http://instantrimshot.com/index.php?sound=catcall">catcall</a>
<a href="http://instantrimshot.com/index.php?sound=clap">clap</a> -->
<a href="http://instantrimshot.com/index.php?sound=cmonson">cmonson</a>
<a href="http://instantrimshot.com/index.php?sound=csi">csi</a>
<a href="http://instantrimshot.com/index.php?sound=coughbull@%&#">coughbull@%&#</a>
<a href="http://instantrimshot.com/index.php?sound=cowbell">cowbell</a>
<a href="http://instantrimshot.com/index.php?sound=crickets">crickets</a>
<a href="http://instantrimshot.com/index.php?sound=downer">downer</a>
<a href="http://instantrimshot.com/index.php?sound=drumroll">drumroll</a>
<!-- <a href="http://instantrimshot.com/index.php?sound=dynamite">dynamite</a> -->
<a href="http://instantrimshot.com/index.php?sound=gobble">gobble</a>
<a href="http://instantrimshot.com/index.php?sound=gong">gong</a>
<!--<a href="http://instantrimshot.com/index.php?sound=heehaw">heehaw</a> -->
<!--<a href="http://instantrimshot.com/index.php?sound=heyheyhey">heyheyhey</a> -->
<a href="http://instantrimshot.com/index.php?sound=howl">howl</a>
<!--<a href="http://instantrimshot.com/index.php?sound=laughing">laughing</a> -->
<a href="http://instantrimshot.com/index.php?sound=priceiswrong">priceiswrong</a>
<a href="http://instantrimshot.com/index.php?sound=reveille">reveille</a>
<a href="http://instantrimshot.com/index.php?sound=readingrainbow">reading rainbow</a>
<!--<a href="http://instantrimshot.com/index.php?sound=recordstop">record stop</a>  -->
<a href="http://instantrimshot.com/index.php?sound=rimshot">rimshot</a>
<a href="http://instantrimshot.com/index.php?sound=rooster">rooster</a>
<a href="http://instantrimshot.com/index.php?sound=slidewhistle">slidewhistle</a>
<!--<a href="http://instantrimshot.com/index.php?sound=slowclap">slowclap</a>
<a href="http://instantrimshot.com/index.php?sound=studioaudience">studio audience</a>
<a href="http://instantrimshot.com/index.php?sound=sweet">sweet</a> -->
<a href="http://instantrimshot.com/index.php?sound=suey">suey</a>
<!--<a href="http://instantrimshot.com/index.php?sound=whipcrack">whipcrack</a>
<a href="http://instantrimshot.com/index.php?sound=whoopee">whoopee</a> -->
<a href="http://instantrimshot.com/index.php?sound=wilhelmscream">wilhelm scream</a>
<a href="http://instantrimshot.com/index.php?sound=yeehaw">yeehaw</a>
Title: Re: Javascript -- you never need to use "new"
Post by: Mr. Analog on December 08, 2012, 10:08:38 AM
I saw those in the header? What browser are you using?
Title: Re: Javascript -- you never need to use "new"
Post by: Darren Dirt on December 08, 2012, 10:44:55 AM
Quote from: Mr. Analog on December 08, 2012, 10:08:38 AM
I saw those in the header? What browser are you using?

(http://catmacros.files.wordpress.com/2009/12/wait_what.jpg)

You mean your browser ignores the <!-- ... --> HTML comment wrapper?

Becase I don't see "dynamite" or "whipcrack" or the others that are hidden. I don't see them as active links on the instantrimshot website anyway (obviously the links in the above forum post are working ... why they're hidden, who knows?).

Title: Re: Javascript -- you never need to use "new"
Post by: Mr. Analog on December 08, 2012, 11:35:38 AM
I didn't parse the whole thing, I just saw the first few paths and realized that I'd seen them.

Next time trim your comment to include the links you're talking about. :P