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

Re: Misapply

Expand Messages
  • sandyhead25
    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
    Message 1 of 5 , May 15, 2012
    View Source
    • 0 Attachment
      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.
      >
    • 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 2 of 5 , May 15, 2012
      View Source
      • 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 3 of 5 , May 15, 2012
        View Source
        • 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 4 of 5 , May 15, 2012
          View Source
          • 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.