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

newcap

Expand Messages
  • Douglas Crockford
    I have added a new option: [ ] Require Initial Caps for constructors (option.newcap) I highly recommend that you always turn this option on. If you call a
    Message 1 of 7 , Nov 26, 2008
    View Source
    • 0 Attachment
      I have added a new option:

      [ ] Require Initial Caps for constructors (option.newcap)

      I highly recommend that you always turn this option on.

      If you call a constructor function without the new prefix, instead of
      creating and initializing a new object, you will be damaging the
      global object. There is no compile time warning and no runtime
      warning. This is one of the language's bad parts.

      In my practice, I completely avoid use of new. That is not yet a
      popular alternative, so if you still rely on the use of new, adopt and
      brutally enforce the convention that every constructor function begin
      with a capital letter, and that nothing else begins with a capital
      letter. That way you and your users at least have a visual cue.
      JSLint's newcap option can enforce this for you.

      If this is so important, then why would you ever want to turn the
      option off?

      Startup performance of page applications is significantly enhanced by
      minifying the code. Obfuscaters can produce a bigger benefit, but at
      the risk of injecting new bugs. This can be mitigated by running the
      obfuscated code through JSLint, which might (but is not guaranteed to)
      find the new bugs. Obfuscaters can do renaming, which could produce
      names that do not conform to the "only constructor functions begin
      with an initial capital" rule. It is for this use that I added the
      newcap option.
    • Merlin
      ... Because the Yahoo! JSLint Widget is now very out of date, I offer my Widget Tester Widget (which includes JSLint) at http://tinyurl.com/5unocx . I
      Message 2 of 7 , Nov 27, 2008
      View Source
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
        >
        > I have added a new option:
        >
        > [ ] Require Initial Caps for constructors (option.newcap)

        Because the Yahoo! JSLint Widget is now very out of date, I offer my Widget Tester Widget
        (which includes JSLint) at http://tinyurl.com/5unocx . I normally try to follow Douglas's
        updates whenever they occur.
      • Chris
        The documentation suggests that this should work: /*jslint newcap: true */ or /*jslint newcap: false */ However, JSLint itself complains about this being a
        Message 3 of 7 , Dec 11, 2008
        View Source
        • 0 Attachment
          The documentation suggests that this should work:

          /*jslint newcap: true */
          or
          /*jslint newcap: false */

          However, JSLint itself complains about this being a "Bad Option."


          --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...>
          wrote:
          >
          > I have added a new option:
          >
          > [ ] Require Initial Caps for constructors (option.newcap)
          >
        • Douglas Crockford
          ... Thanks for the report.
          Message 4 of 7 , Dec 11, 2008
          View Source
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "Chris" <Nielsen.Chris@...> wrote:
            >
            > The documentation suggests that this should work:
            >
            > /*jslint newcap: true */
            > or
            > /*jslint newcap: false */
            >
            > However, JSLint itself complains about this being a "Bad Option."

            Thanks for the report.
          • Julien Royer
            Hi, Why does JSLint use the following regexp to detect names beginning with a capital letter (newcap option)? /^[A-Z]([A-Z0-9_$]*[a-z][A-Za-z0-9_$]*)?$/ It
            Message 5 of 7 , Feb 4, 2010
            View Source
            • 0 Attachment
              Hi,

              Why does JSLint use the following regexp to detect names beginning
              with a capital letter (newcap option)?
              /^[A-Z]([A-Z0-9_$]*[a-z][A-Za-z0-9_$]*)?$/

              It makes the following names valid for non-constructor functions:
              "T1", "HTML", "H$1", ...
            • Woomla
              ... I m just curious what you mean by this. How do you create objects then?
              Message 6 of 7 , Feb 9, 2010
              View Source
              • 0 Attachment
                --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
                > In my practice, I completely avoid use of new.

                I'm just curious what you mean by this. How do you create objects then?
              • Douglas Crockford
                ... I use object literals and Object.create(prototype). Object.create is in ES5. It can be added to ES3 with if (!Object.create) { Object.create = function
                Message 7 of 7 , Feb 9, 2010
                View Source
                • 0 Attachment
                  --- In jslint_com@yahoogroups.com, "Woomla" <woomla@...> wrote:
                  >
                  > --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@> wrote:
                  > > In my practice, I completely avoid use of new.
                  >
                  > I'm just curious what you mean by this. How do you create objects then?


                  I use object literals and Object.create(prototype). Object.create is in ES5. It can be added to ES3 with

                  if (!Object.create) {
                  Object.create = function (prototype) {
                  function F() {}
                  F.prototype = prototype;
                  return new F();
                  };
                  }
                Your message has been successfully submitted and would be delivered to recipients shortly.