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: 584
  • Category: JavaScript
  • Founded: Mar 7, 2008
  • Language: English
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Messages

Advanced
Messages Help
Misapply   Topic List   < Prev Topic  |  Next Topic >
Summarize Messages Sort by Date  
#2885 From: "douglascrockford" <douglas@...>
Date: Mon May 14, 2012 6:30 pm
Subject: Misapply
douglascrock...
Send Email Send Email
 
This code fails JSLint:

````var myScript = {};
````(function () {
````````var THIS = this;
````````function defined(x) {
````````````return typeof x !== 'undefined';
````````}
````````this.ready = false;
````````this.init = function () {
````````````this.ready = true;
````````};
````````this.doSomething = function () {};
````````var options = {
````````````x : 123,
````````````y : 'abc'
````````};
````````this.define = function (key, value) {
````````````if (defined(options[key])) {
````````````````options[key] = value;
````````````}
````````};
````}).apply(myScript);

The warning is "Unexpected '.'" on the last line. This is an overly complicated,
inefficent variant of the module pattern. There is a good idea in it, but it is
undone an odd desire to use -this- in an unexpected way. It creates a variable
called -THIS- to circumvent one of the problems with -this-.

This modification eliminates the use of apply, making the code a little smaller
and faster. Also, by avoiding -this-, there is no need for -THIS-.

````var myScript = {};
````(function (that) {
````````function defined(x) {
````````````return typeof x !== 'undefined';
````````}
````````that.ready = false;
````````that.init = function () {
````````````that.ready = true;
````````};
````````that.doSomething = function () {};
````````var options = {
````````````x : 123,
````````````y : 'abc'
````````};
````````that.define = function (key, value) {
````````````if (defined(options[key])) {
````````````````options[key] = value;
````````````}
````````};
````}(myScript));

But we can make it even simpler. Since this function can only ever be called
once, there is little advantage to parameterizing it.

````var myScript = {};
````(function () {
````````function defined(x) {
````````````return typeof x !== 'undefined';
````````}
````````myScript.ready = false;
````````myScript.init = function () {
````````````myScript.ready = true;
````````};
````````myScript.doSomething = function () {};
````````var options = {
````````````x : 123,
````````````y : 'abc'
````````};
````````myScript.define = function (key, value) {
````````````if (defined(options[key])) {
````````````````options[key] = value;
````````````}
````````};
````}());

The original code came from
http://www.sitepoint.com/my-favorite-javascript-design-pattern/, and has been
highly praised in http://msdn.microsoft.com/en-us/magazine/gg578608.aspx

When JSLint warns you about code like this, remember that it is trying to help
you.





#2886 From: "sandyhead25" <austin.cheney@...>
Date: Tue May 15, 2012 1:49 pm
Subject: Re: Misapply
sandyhead25
Send Email Send Email
 
Would it be possible to warn on use of the "this" keyword?  With exception to
extending prototypes or referencing from the DOM I have never seen an efficient
use of the "this" keyword. I would even go further to say that its use in
writing unnecessarily obtuse circular logic is common, typically referred to as
a constructor, and is harmful to learning the language. Harmful to the extent
of being backwards and confusing experienced programmers of whom are new to
JavaScript into thinking they are somehow writing some Java class, which thereby
misrepresents and ignores learnings of the simple and more elegant features
offered by the language.

Warning on the "this" keyword would then eliminate most of the inefficient use
cases of bind and apply methods.

Austin

--- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
>
> This code fails JSLint:
>
> ````var myScript = {};
> ````(function () {
> ````````var THIS = this;
> ````````function defined(x) {
> ````````````return typeof x !== 'undefined';
> ````````}
> ````````this.ready = false;
> ````````this.init = function () {
> ````````````this.ready = true;
> ````````};
> ````````this.doSomething = function () {};
> ````````var options = {
> ````````````x : 123,
> ````````````y : 'abc'
> ````````};
> ````````this.define = function (key, value) {
> ````````````if (defined(options[key])) {
> ````````````````options[key] = value;
> ````````````}
> ````````};
> ````}).apply(myScript);
>
> The warning is "Unexpected '.'" on the last line. This is an overly
complicated, inefficent variant of the module pattern. There is a good idea in
it, but it is undone an odd desire to use -this- in an unexpected way. It
creates a variable called -THIS- to circumvent one of the problems with -this-.
>
> This modification eliminates the use of apply, making the code a little
smaller and faster. Also, by avoiding -this-, there is no need for -THIS-.
>
> ````var myScript = {};
> ````(function (that) {
> ````````function defined(x) {
> ````````````return typeof x !== 'undefined';
> ````````}
> ````````that.ready = false;
> ````````that.init = function () {
> ````````````that.ready = true;
> ````````};
> ````````that.doSomething = function () {};
> ````````var options = {
> ````````````x : 123,
> ````````````y : 'abc'
> ````````};
> ````````that.define = function (key, value) {
> ````````````if (defined(options[key])) {
> ````````````````options[key] = value;
> ````````````}
> ````````};
> ````}(myScript));
>
> But we can make it even simpler. Since this function can only ever be called
once, there is little advantage to parameterizing it.
>
> ````var myScript = {};
> ````(function () {
> ````````function defined(x) {
> ````````````return typeof x !== 'undefined';
> ````````}
> ````````myScript.ready = false;
> ````````myScript.init = function () {
> ````````````myScript.ready = true;
> ````````};
> ````````myScript.doSomething = function () {};
> ````````var options = {
> ````````````x : 123,
> ````````````y : 'abc'
> ````````};
> ````````myScript.define = function (key, value) {
> ````````````if (defined(options[key])) {
> ````````````````options[key] = value;
> ````````````}
> ````````};
> ````}());
>
> The original code came from
http://www.sitepoint.com/my-favorite-javascript-design-pattern/, and has been
highly praised in http://msdn.microsoft.com/en-us/magazine/gg578608.aspx
>
> When JSLint warns you about code like this, remember that it is trying to help
you.
>





#2887 From: Robert Ferney <capnregex@...>
Date: Tue May 15, 2012 4:52 pm
Subject: Re: [jslint] Misapply
capnregex
Send Email Send Email
 
Douglas,

This little explanation was great.
It would be wonderful if the error messages would link to pages with
little explanations like this.

- Robert Ferney



On Mon, May 14, 2012 at 12:30 PM, douglascrockford <douglas@...>wrote:

> **
>
>
> This code fails JSLint:
>
> ````var myScript = {};
> ````(function () {
> ````````var THIS = this;
> ````````function defined(x) {
> ````````````return typeof x !== 'undefined';
> ````````}
> ````````this.ready = false;
> ````````this.init = function () {
> ````````````this.ready = true;
> ````````};
> ````````this.doSomething = function () {};
> ````````var options = {
> ````````````x : 123,
> ````````````y : 'abc'
> ````````};
> ````````this.define = function (key, value) {
> ````````````if (defined(options[key])) {
> ````````````````options[key] = value;
> ````````````}
> ````````};
> ````}).apply(myScript);
>
> The warning is "Unexpected '.'" on the last line. This is an overly
> complicated, inefficent variant of the module pattern. There is a good idea
> in it, but it is undone an odd desire to use -this- in an unexpected way.
> It creates a variable called -THIS- to circumvent one of the problems with
> -this-.
>
> This modification eliminates the use of apply, making the code a little
> smaller and faster. Also, by avoiding -this-, there is no need for -THIS-.
>
> ````var myScript = {};
> ````(function (that) {
> ````````function defined(x) {
> ````````````return typeof x !== 'undefined';
> ````````}
> ````````that.ready = false;
> ````````that.init = function () {
> ````````````that.ready = true;
> ````````};
> ````````that.doSomething = function () {};
> ````````var options = {
> ````````````x : 123,
> ````````````y : 'abc'
> ````````};
> ````````that.define = function (key, value) {
> ````````````if (defined(options[key])) {
> ````````````````options[key] = value;
> ````````````}
> ````````};
> ````}(myScript));
>
> But we can make it even simpler. Since this function can only ever be
> called once, there is little advantage to parameterizing it.
>
> ````var myScript = {};
> ````(function () {
> ````````function defined(x) {
> ````````````return typeof x !== 'undefined';
> ````````}
> ````````myScript.ready = false;
> ````````myScript.init = function () {
> ````````````myScript.ready = true;
> ````````};
> ````````myScript.doSomething = function () {};
> ````````var options = {
> ````````````x : 123,
> ````````````y : 'abc'
> ````````};
> ````````myScript.define = function (key, value) {
> ````````````if (defined(options[key])) {
> ````````````````options[key] = value;
> ````````````}
> ````````};
> ````}());
>
> The original code came from
> http://www.sitepoint.com/my-favorite-javascript-design-pattern/, and has
> been highly praised in
> http://msdn.microsoft.com/en-us/magazine/gg578608.aspx
>
> When JSLint warns you about code like this, remember that it is trying to
> help you.
>
>
>


[Non-text portions of this message have been removed]




#2888 From: Robert Ferney <capnregex@...>
Date: Tue May 15, 2012 4:54 pm
Subject: Re: [jslint] Misapply
capnregex
Send Email Send Email
 
Or even, references to the chapter in The Good Parts.

- Robert Ferney



On Tue, May 15, 2012 at 10:52 AM, Robert Ferney <capnregex@...> wrote:

> Douglas,
>
> This little explanation was great.
> It would be wonderful if the error messages would link to pages with
> little explanations like this.
>
> - Robert Ferney
>
>
>
> On Mon, May 14, 2012 at 12:30 PM, douglascrockford
<douglas@...>wrote:
>
>> **
>>
>>
>> This code fails JSLint:
>>
>> ````var myScript = {};
>> ````(function () {
>> ````````var THIS = this;
>> ````````function defined(x) {
>> ````````````return typeof x !== 'undefined';
>> ````````}
>> ````````this.ready = false;
>> ````````this.init = function () {
>> ````````````this.ready = true;
>> ````````};
>> ````````this.doSomething = function () {};
>> ````````var options = {
>> ````````````x : 123,
>> ````````````y : 'abc'
>> ````````};
>> ````````this.define = function (key, value) {
>> ````````````if (defined(options[key])) {
>> ````````````````options[key] = value;
>> ````````````}
>> ````````};
>> ````}).apply(myScript);
>>
>> The warning is "Unexpected '.'" on the last line. This is an overly
>> complicated, inefficent variant of the module pattern. There is a good idea
>> in it, but it is undone an odd desire to use -this- in an unexpected way.
>> It creates a variable called -THIS- to circumvent one of the problems with
>> -this-.
>>
>> This modification eliminates the use of apply, making the code a little
>> smaller and faster. Also, by avoiding -this-, there is no need for -THIS-.
>>
>> ````var myScript = {};
>> ````(function (that) {
>> ````````function defined(x) {
>> ````````````return typeof x !== 'undefined';
>> ````````}
>> ````````that.ready = false;
>> ````````that.init = function () {
>> ````````````that.ready = true;
>> ````````};
>> ````````that.doSomething = function () {};
>> ````````var options = {
>> ````````````x : 123,
>> ````````````y : 'abc'
>> ````````};
>> ````````that.define = function (key, value) {
>> ````````````if (defined(options[key])) {
>> ````````````````options[key] = value;
>> ````````````}
>> ````````};
>> ````}(myScript));
>>
>> But we can make it even simpler. Since this function can only ever be
>> called once, there is little advantage to parameterizing it.
>>
>> ````var myScript = {};
>> ````(function () {
>> ````````function defined(x) {
>> ````````````return typeof x !== 'undefined';
>> ````````}
>> ````````myScript.ready = false;
>> ````````myScript.init = function () {
>> ````````````myScript.ready = true;
>> ````````};
>> ````````myScript.doSomething = function () {};
>> ````````var options = {
>> ````````````x : 123,
>> ````````````y : 'abc'
>> ````````};
>> ````````myScript.define = function (key, value) {
>> ````````````if (defined(options[key])) {
>> ````````````````options[key] = value;
>> ````````````}
>> ````````};
>> ````}());
>>
>> The original code came from
>> http://www.sitepoint.com/my-favorite-javascript-design-pattern/, and has
>> been highly praised in
>> http://msdn.microsoft.com/en-us/magazine/gg578608.aspx
>>
>> When JSLint warns you about code like this, remember that it is trying to
>> help you.
>>
>>
>>
>
>


[Non-text portions of this message have been removed]




#2889 From: Tom Worster <fsb@...>
Date: Tue May 15, 2012 5:04 pm
Subject: Re: [jslint] Misapply
thefsb
Send Email Send Email
 
On 5/15/12 12:52 PM, "Robert Ferney" <capnregex@...> wrote:

>This little explanation was great.
>It would be wonderful if the error messages would link to pages with
>little explanations like this.

I agree and support the feature request.

Sometimes I feel so stupid reading fancy modern JS code. It's nice to know
I'm not alone in benefiting from "the easy version".





 
Add to My Yahoo!      XML What's This?

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