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

Re: [jslint] Re: Strict violation

Expand Messages
  • Nagy Endre
    Try instead this: use strict ; var foo = function() { return this.message; }; foo.call({ message: Hello! }); or this use strict ; var foo= function() {
    Message 1 of 14 , Oct 17, 2010
      Try instead this:

      "use strict";

      var foo = function() {
      return this.message;
      };

      foo.call({ message: "Hello!" });

      or this

      "use strict";

      var foo= function() {
      return this.message;
      };

      document.body.addEventListener("click", foo, true);



      ________________________________
      From: pauanyu <pcxunlimited@...>
      To: jslint_com@yahoogroups.com
      Sent: Sun, October 17, 2010 9:05:54 PM
      Subject: [jslint] Re: Strict violation


      --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
      >
      > --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@> wrote:
      > >
      > > --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@> wrote:
      > > >
      > > > --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@> wrote:
      > > > >
      > > > > "use strict";
      > > > >
      > > > > function foo() {
      > > > > return this.message;
      > > > > }
      > > > >
      > > > > foo.call({ message: "Hello!" });
      > > > >
      > > > >
      > > > > The above returns the error "Problem at line 4 character 12: Strict
      >violation."
      > > > >
      > > > > Are the call and apply methods no longer allowed in ECMAScript 5 strict
      >mode?
      > > >
      > > > The warning was on line 4, not on line 6.
      > > >
      > >
      > > Indeed, which is why I said it was on line 4.
      > >
      > > My point is that JSLint is thinking that the "this" usage is referring to the
      >global object, when in fact it is not (because of `call`). This means that you
      >cannot use `call` or `apply` with strict mode without JSLint spitting back an
      >error.
      > >
      > > So I ask again: are call and apply banned in ECMAScript 5 strict mode, or
      >should JSLint tolerate this?
      >
      >
      > No. That is why the error was not on line 6.
      >

      "use strict";

      function foo() {
      return this.message;
      }

      document.body.addEventListener("click", foo, true);

      The above returns the same error, at the same location. What do I need to do to
      convince you that this is a problem? I think JSLint should tolerate "this" when
      it is being used in these two ways.

      Both ways should be valid, according to ECMAScript 5 strict mode, so unless you
      have a reason to believe that these two use-cases should be banned...







      [Non-text portions of this message have been removed]
    • Douglas Crockford
      ... There is a lot of crappy code that ES5 accepts that JSLint rejects. Your argument will not work here. The point of ES5/strict is to prohibit leaking of the
      Message 2 of 14 , Oct 18, 2010
        --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@...> wrote:

        > "use strict";
        >
        > function foo() {
        > return this.message;
        > }
        >
        > document.body.addEventListener("click", foo, true);
        >
        >
        > The above returns the same error, at the same location. What do I need to do to convince you that this is a problem? I think JSLint should tolerate "this" when it is being used in these two ways.
        >
        > Both ways should be valid, according to ECMAScript 5 strict mode, so unless you have a reason to believe that these two use-cases should be banned...


        There is a lot of crappy code that ES5 accepts that JSLint rejects. Your argument will not work here.

        The point of ES5/strict is to prohibit leaking of the global object, something that ES3 does promiscuously. ES5/strict does some of its work dynamically, and some of its work statically. JSLint does all of its work statically, so it must be even more restrictive in order to best help you get your program right. Remember that the goal of JSLint is to improve code quality, not to make you feel good about sloppy work.

        So when JSLint sees you saying "use strict"; and then sees you use a function statement containing this, it assumes that you don't know what you are doing and it gives you a warning.

        My advice is that you either stop using strict mode, or adopt a more professional coding style.
      • pauanyu
        ... In that case, could you at least change the error message to something like: Strict violation: don t use this within a function.
        Message 3 of 14 , Oct 19, 2010
          --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
          >
          > --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@> wrote:
          >
          > > "use strict";
          > >
          > > function foo() {
          > > return this.message;
          > > }
          > >
          > > document.body.addEventListener("click", foo, true);
          > >
          > >
          > > The above returns the same error, at the same location. What do I need to do to convince you that this is a problem? I think JSLint should tolerate "this" when it is being used in these two ways.
          > >
          > > Both ways should be valid, according to ECMAScript 5 strict mode, so unless you have a reason to believe that these two use-cases should be banned...
          >
          >
          > There is a lot of crappy code that ES5 accepts that JSLint rejects. Your argument will not work here.
          >
          > The point of ES5/strict is to prohibit leaking of the global object, something that ES3 does promiscuously. ES5/strict does some of its work dynamically, and some of its work statically. JSLint does all of its work statically, so it must be even more restrictive in order to best help you get your program right. Remember that the goal of JSLint is to improve code quality, not to make you feel good about sloppy work.
          >
          > So when JSLint sees you saying "use strict"; and then sees you use a function statement containing this, it assumes that you don't know what you are doing and it gives you a warning.
          >
          > My advice is that you either stop using strict mode, or adopt a more professional coding style.
          >

          In that case, could you at least change the error message to something like:

          Strict violation: don't use "this" within a function.
        • Michael
          ... A full explanation would really be helpful. This was something that was really confusing me just this morning. Especially with a lot of libraries which
          Message 4 of 14 , Oct 19, 2010
            On Tue, Oct 19, 2010 at 3:17 PM, pauanyu <pcxunlimited@...> wrote:

            >
            >
            > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>, "Douglas
            > Crockford" <douglas@...> wrote:
            > >
            > > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>,
            > "pauanyu" <pcxunlimited@> wrote:
            > >
            > > > "use strict";
            > > >
            > > > function foo() {
            > > > return this.message;
            > > > }
            > > >
            > > > document.body.addEventListener("click", foo, true);
            > > >
            > > >
            > > > The above returns the same error, at the same location. What do I need
            > to do to convince you that this is a problem? I think JSLint should tolerate
            > "this" when it is being used in these two ways.
            > > >
            > > > Both ways should be valid, according to ECMAScript 5 strict mode, so
            > unless you have a reason to believe that these two use-cases should be
            > banned...
            > >
            > >
            > > There is a lot of crappy code that ES5 accepts that JSLint rejects. Your
            > argument will not work here.
            > >
            > > The point of ES5/strict is to prohibit leaking of the global object,
            > something that ES3 does promiscuously. ES5/strict does some of its work
            > dynamically, and some of its work statically. JSLint does all of its work
            > statically, so it must be even more restrictive in order to best help you
            > get your program right. Remember that the goal of JSLint is to improve code
            > quality, not to make you feel good about sloppy work.
            > >
            > > So when JSLint sees you saying "use strict"; and then sees you use a
            > function statement containing this, it assumes that you don't know what you
            > are doing and it gives you a warning.
            > >
            > > My advice is that you either stop using strict mode, or adopt a more
            > professional coding style.
            > >
            >
            > In that case, could you at least change the error message to something
            > like:
            >
            > Strict violation: don't use "this" within a function.
            >

            A full explanation would really be helpful. This was something that was
            really confusing me just this morning. Especially with a lot of libraries
            which show examples of applying a scope to a callback function. Looking at
            it now explicit is beautiful, but as a long time JS programmer, I always
            thought the other way was better until I did research and read into some of
            Douglas's comments in this thread.

            Thanks,

            Michael


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