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

Strict violation

Expand Messages
  • Aseem
    Hi there, Can you please explain why this is a strict violation ? use strict ; (function (window) { // ... }(this)); I ve seen this as a common technique
    Message 1 of 14 , Sep 24, 2009
    View Source
    • 0 Attachment
      Hi there,

      Can you please explain why this is a "strict violation"?

      "use strict";
      (function (window) {
      // ...
      }(this));

      I've seen this as a common technique used by various libraries (incl. jQuery) to allow munging of the window name inside the closure.

      Is it some bad use of the global object? Thanks!

      Aseem
    • Douglas Crockford
      ... Strict mode does not allow use of top-level this. you may want to turn off the strict option.
      Message 2 of 14 , Sep 24, 2009
      View Source
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "Aseem" <aseem.kishore@...> wrote:

        > Can you please explain why this is a "strict violation"?
        >
        > "use strict";
        > (function (window) {
        > // ...
        > }(this));
        >
        > I've seen this as a common technique used by various libraries (incl.
        > jQuery) to allow munging of the window name inside the closure.

        Strict mode does not allow use of top-level this. you may want to turn off the strict option.
      • Chris
        ... (incl. ... off the strict option. ... Hello, A similar situation came up for me today. Can anybody elaborate on where this is documented? I m looking in
        Message 3 of 14 , Sep 20, 2010
        View Source
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...>
          wrote:
          >
          > --- In jslint_com@yahoogroups.com, "Aseem" aseem.kishore@ wrote:
          >
          > > Can you please explain why this is a "strict violation"?
          > >
          > > "use strict";
          > > (function (window) {
          > > // ...
          > > }(this));
          > >
          > > I've seen this as a common technique used by various libraries
          (incl.
          > > jQuery) to allow munging of the window name inside the closure.
          >
          > Strict mode does not allow use of top-level this. you may want to turn
          off the strict option.
          >

          Hello,

          A similar situation came up for me today. Can anybody elaborate on
          where this is documented? I'm looking in the ES5 specifications
          <http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.p\
          df> , Annex C, and the closest thing I see is this:

          If this is evaluated within strict mode code, then the this value is not
          coerced to an object. A this value of null or undefined is not converted
          to the global object and primitive values are not converted to wrapper
          objects. The this value passed via a function call (including calls made
          using Function.prototype.apply and Function.prototype.call) do not
          coerce the passed this value to an object (10.4.3, 11.1.1, 15.3.4.3,
          15.3.4.4).

          However, I read this as meaning that someFunction.call(undefined) will
          no longer execute someFunction with "this" bound to the global object.
          I do not see where it indicates that using "this" in the global scope
          (as in Aseem's example) will cause a problem by itself.

          What am I missing?

          Thanks!

          - Chris



          [Non-text portions of this message have been removed]
        • pauanyu
          use strict ; function foo() { return this.message; } foo.call({ message: Hello! }); The above returns the error Problem at line 4 character 12: Strict
          Message 4 of 14 , Oct 7, 2010
          View Source
          • 0 Attachment
            "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?
          • Douglas Crockford
            ... The warning was on line 4, not on line 6.
            Message 5 of 14 , Oct 10, 2010
            View Source
            • 0 Attachment
              --- 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.
            • pauanyu
              ... 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
              Message 6 of 14 , Oct 13, 2010
              View Source
              • 0 Attachment
                --- 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?
              • Douglas Crockford
                ... No. That is why the error was not on line 6.
                Message 7 of 14 , Oct 16, 2010
                View Source
                • 0 Attachment
                  --- 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.
                • Stefan Weiss
                  ... Excuse me, but that s not an answer to pauanyu s question. call() and apply() still exist in ES5, even in strict mode, and no static code analysis tool can
                  Message 8 of 14 , Oct 16, 2010
                  View Source
                  • 0 Attachment
                    On 17/10/10 02:12, Douglas Crockford wrote:
                    > --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@...> wrote:
                    >> 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.

                    Excuse me, but that's not an answer to pauanyu's question. call() and
                    apply() still exist in ES5, even in strict mode, and no static code
                    analysis tool can possibly determine what -this- refers to in a function
                    like

                    function foo() {
                    return this.message;
                    }

                    Throwing an unconditional error here is not a valid strategy for a
                    linting tool, unless you can explain why, and how to avoid the error. I
                    realize that you're very busy, but the question was valid and, IMHO,
                    deserves a better explanation.


                    regards,
                    stefan
                  • Dominic Mitchell
                    ... I think the problem here is the this keyword. From the spec , Annex C: *A this
                    Message 9 of 14 , Oct 17, 2010
                    View Source
                    • 0 Attachment
                      On Sun, Oct 17, 2010 at 3:30 AM, Stefan Weiss <weiss@...> wrote:

                      > Excuse me, but that's not an answer to pauanyu's question. call() and
                      > apply() still exist in ES5, even in strict mode, and no static code
                      > analysis tool can possibly determine what -this- refers to in a function
                      > like
                      >
                      > function foo() {
                      > return this.message;
                      > }
                      >
                      > Throwing an unconditional error here is not a valid strategy for a
                      > linting tool, unless you can explain why, and how to avoid the error. I
                      > realize that you're very busy, but the question was valid and, IMHO,
                      > deserves a better explanation.
                      >

                      I think the problem here is the this keyword. From the
                      spec<http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf>,
                      Annex C:

                      *A this value of null or undefined is not converted to the global object and
                      primitive values are not converted to wrapper objects.*


                      Because that function isn't being used in an object context (it's not a
                      constructor as it doesn't start with a capital), JSLint is warning about the
                      use of this.

                      -Dom


                      [Non-text portions of this message have been removed]
                    • pauanyu
                      ... use strict ; function foo() { return this.message; } document.body.addEventListener( click , foo, true); The above returns the same error, at the same
                      Message 10 of 14 , Oct 17, 2010
                      View Source
                      • 0 Attachment
                        --- 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...
                      • 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 11 of 14 , Oct 17, 2010
                        View Source
                        • 0 Attachment
                          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 12 of 14 , Oct 18, 2010
                          View Source
                          • 0 Attachment
                            --- 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 13 of 14 , Oct 19, 2010
                            View Source
                            • 0 Attachment
                              --- 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 14 of 14 , Oct 19, 2010
                              View Source
                              • 0 Attachment
                                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.