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

Re: [jslint] bad constructor?

Expand Messages
  • Stjepan Rajko
    Luke, thanks for your reply. The warnings were generated by jslint.com with Require Initial Caps for constructors turned off, and adding /*jslint newcap:
    Message 1 of 6 , Mar 9, 2011
    View Source
    • 0 Attachment
      Luke, thanks for your reply.

      The warnings were generated by jslint.com with "Require Initial Caps for
      constructors" turned off, and adding /*jslint newcap: false */ to the code
      in my own environment didn't make a difference (but this tip was really
      helpful - I didn't know you could turn options on/off for a specific
      function!).

      How are you getting jslint to influence the code snippet I submitted via
      newcap? The description for newcap in the instructions seems to focus on the
      constructor function being capitalized.

      2. would work, but I'm trying to keep the code terse / avoid listing the
      same arguments twice.

      Best,

      Stjepan

      On Wed, Mar 9, 2011 at 9:05 AM, Luke Page <luke.a.page@...> wrote:

      > jslint cannot determine that the expression One relating to a function will
      > be used to construct a new object. You have three options.
      >
      > This is controlled by the newcap option as described on the jslint
      > instructions page.
      >
      > 1. turn off newcap
      > 2. use an if block
      > var x;
      > if (which) {
      > x = new One(["1", "2", "3"]);
      > } else {
      > x = new Two(["1", "2", "3"]);
      > }
      > 3. Depending on your real use, and if it makes sense, wrap up the
      > calling/construction of One, Two etc. into a function that abstracts your
      > purpose re: using an option to choose what object to instantiate. Then turn
      > off newcap for this function e.g.
      >
      > var GetObjectfunction = function(args) {
      > /*jslint newcap: false */
      > return new (which ? One : Two) (args);
      > }
      >
      > Regards,
      >
      > Luke
      >
      > On 9 March 2011 15:13, Stjepan Rajko <stjepan.rajko@...> wrote:
      >
      >>
      >> 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]
      >>
      >>
      >>
      >
      >


      [Non-text portions of this message have been removed]
    • 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 2 of 6 , Mar 9, 2011
      View Source
      • 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 3 of 6 , Mar 9, 2011
        View Source
        • 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 4 of 6 , Mar 9, 2011
          View Source
          • 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.