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

Re: [jslint] Misapply

Expand Messages
  • Robert Ferney
    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
    Message 1 of 5 , May 15, 2012
    • 0 Attachment
      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]
    • Robert Ferney
      Or even, references to the chapter in The Good Parts. - Robert Ferney ... [Non-text portions of this message have been removed]
      Message 2 of 5 , May 15, 2012
      • 0 Attachment
        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]
      • Tom Worster
        ... 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
        Message 3 of 5 , May 15, 2012
        • 0 Attachment
          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".
        Your message has been successfully submitted and would be delivered to recipients shortly.