Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

jslint_com · This group has moved to Google Plus.

The Yahoo! Groups Product Blog

Check it out!

Group Information

  • Members: 583
  • Category: JavaScript
  • Founded: Mar 7, 2008
  • Language: English
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Messages

Advanced
Messages Help
Strict violation   Topic List   < Prev Topic  |  Next Topic >
Summarize Messages Sort by Date  
#927 From: "Aseem" <aseem.kishore@...>
Date: Fri Sep 25, 2009 12:25 am
Subject: Strict violation
aseem.kishor...
Send Email Send Email
 
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




#928 From: "Douglas Crockford" <douglas@...>
Date: Fri Sep 25, 2009 12:41 am
Subject: Re: Strict violation
douglascrock...
Send Email Send Email
 
--- 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.




#1500 From: "Chris" <Nielsen.Chris@...>
Date: Tue Sep 21, 2010 3:53 am
Subject: Re: Strict violation
altearius
Send Email Send Email
 

--- 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]




#1512 From: "pauanyu" <pcxunlimited@...>
Date: Thu Oct 7, 2010 8:22 pm
Subject: Strict violation
pauanyu
Send Email Send Email
 
"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?




#1515 From: "Douglas Crockford" <douglas@...>
Date: Sun Oct 10, 2010 11:45 am
Subject: Re: Strict violation
douglascrock...
Send Email Send Email
 
--- 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.




#1533 From: "pauanyu" <pcxunlimited@...>
Date: Wed Oct 13, 2010 8:55 am
Subject: Re: Strict violation
pauanyu
Send Email Send Email
 
--- 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?




#1548 From: "Douglas Crockford" <douglas@...>
Date: Sun Oct 17, 2010 12:12 am
Subject: Re: Strict violation
douglascrock...
Send Email Send Email
 
--- 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.





#1549 From: Stefan Weiss <weiss@...>
Date: Sun Oct 17, 2010 2:30 am
Subject: Re: [jslint] Re: Strict violation
weiss@...
Send Email Send Email
 
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



#1550 From: Dominic Mitchell <dom@...>
Date: Sun Oct 17, 2010 8:31 am
Subject: Re: [jslint] Re: Strict violation
happygiraffe...
Send Email Send Email
 
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]




#1551 From: "pauanyu" <pcxunlimited@...>
Date: Sun Oct 17, 2010 6:05 pm
Subject: Re: Strict violation
pauanyu
Send Email Send Email
 
--- 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...




#1552 From: Nagy Endre <forewer2000@...>
Date: Sun Oct 17, 2010 8:54 pm
Subject: Re: [jslint] Re: Strict violation
forewer2000
Send Email Send Email
 
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]




#1553 From: "Douglas Crockford" <douglas@...>
Date: Mon Oct 18, 2010 4:49 pm
Subject: Re: Strict violation
douglascrock...
Send Email Send Email
 
--- 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.




#1555 From: "pauanyu" <pcxunlimited@...>
Date: Tue Oct 19, 2010 7:17 pm
Subject: Re: Strict violation
pauanyu
Send Email Send Email
 
--- 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.




#1556 From: Michael <newmaniese@...>
Date: Tue Oct 19, 2010 7:30 pm
Subject: Re: [jslint] Re: Strict violation
newmaniese
Send Email Send Email
 
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]




 
Add to My Yahoo!      XML What's This?

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines NEW - Help