Loading ...
Sorry, an error occurred while loading the content.

Re: bad constructor?

Expand Messages
  • Alexandre Morgaut
    You may also write the code this way var Which = Math.round(Math.random()) ? One : Two; var x = new Which( 1 , 2 , 3 ); ;-)
    Message 1 of 6 , Mar 9, 2011
    • 0 Attachment
      You may also write the code this way

      var Which = Math.round(Math.random()) ? One : Two;
      var x = new Which("1", "2", "3");

      ;-)
    • Stjepan Rajko
      ... That takes care of the error, thank you! :-) [Non-text portions of this message have been removed]
      Message 2 of 6 , Mar 9, 2011
      • 0 Attachment
        On Wed, Mar 9, 2011 at 9:58 AM, Alexandre Morgaut <morgaut@...>wrote:

        >
        >
        > You may also write the code this way
        >
        > var Which = Math.round(Math.random()) ? One : Two;
        > var x = new Which("1", "2", "3");
        >
        >
        That takes care of the error, thank you! :-)


        [Non-text portions of this message have been removed]
      • Rob Richardson
        Your code is a perfect example of clever -- one of the best tools a coder has for showing their ingenuity and skill while simultaneously confusing people
        Message 3 of 6 , Mar 9, 2011
        • 0 Attachment
          Your code is a perfect example of "clever" -- one of the best tools a coder
          has for showing their ingenuity and skill while simultaneously confusing
          people reading the code later. I grant that it is more lengthily to make
          your intentions clear, but to do so would almost certainly lead to a faster
          understanding for you and others in a month when you're debugging it. Use
          this rule of thumb consistently and it'll lead to higher productivity and
          lower maintenance costs for all involved.

          Rob


          -----Original Message-----
          From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On
          Behalf Of Stjepan Rajko
          Sent: Wednesday, March 09, 2011 8:13 AM
          To: jslint_com@yahoogroups.com
          Subject: [jslint] bad constructor?

          Hello,

          On the following code:

          function One(a, b, c) {
          ````this.value = a + b + c;
          }
          function Two(a, b, c) {
          ````this.value = c + b + a;
          }
          var which = Math.round(Math.random());
          var x = new (which ? One : Two) ("1", "2", "3");
          alert(x.value);

          JSLint gives:

          *Error:*

          Problem at line 8 character 31: Bad constructor.

          var x = new (which ? One : Two) ("1", "2", "3");
          **
          After a brief look at the ECMAscript spec I think this is valid code but I'm
          not 100% sure. In any case, I was wondering if anyone could help me
          understand why JSLint complains about this. It does seem to work as
          intended (randomly alerts either "123" or "321") in all browsers I tested.

          Thanks,

          Stjepan


          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.