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

Type confusion error when different objects' properties have different types

Expand Messages
  • Jordan
    The following code: /*global other_object */ /*global other_object */ var obj = { callback: function (data) { if (typeof (data.prop) === string ) {
    Message 1 of 12 , Jun 20, 2011
    • 0 Attachment
      The following code:

      /*global other_object */
      /*global other_object */
      var obj = {
      callback: function (data) {
      if (typeof (data.prop) === 'string') {
      other_object.prop('Property: ' + data.prop);
      }
      }
      };

      produces the following error:

      Problem at line 5 character 46: Type confusion: string and function.
      other_object.prop('Property: ' + data.prop);

      This only seems to happen when "other_object" is listed as a global and defined in another file - when I define it in the same file there is no error. In addition, if I replace the `if` condition with "if (typeof (data.prop) === 'string') {" (ie, even more explicitly defining the type of `data.prop`) it produces the same JSLint error.

      - Jordan
    • Douglas Crockford
      The property .prop is being used as both a string and a function.
      Message 2 of 12 , Jun 20, 2011
      • 0 Attachment
        The property .prop is being used as both a string and a function.



        On 6/20/2011 2:34 AM, Jordan wrote:
        > The following code:
        >
        > /*global other_object */
        > /*global other_object */
        > var obj = {
        > callback: function (data) {
        > if (typeof (data.prop) === 'string') {
        > other_object.prop('Property: ' + data.prop);
        > }
        > }
        > };
        >
        > produces the following error:
        >
        > Problem at line 5 character 46: Type confusion: string and function.
        > other_object.prop('Property: ' + data.prop);
        >
        > This only seems to happen when "other_object" is listed as a global and defined in another file - when I define it in the same file there is no error. In addition, if I replace the `if` condition with "if (typeof (data.prop) === 'string') {" (ie, even more explicitly defining the type of `data.prop`) it produces the same JSLint error.
        >
        > - Jordan
        >
        >
        >
        > ------------------------------------
        >
        > Yahoo! Groups Links
        >
        >
        >
        >
      • Jordan
        No, `other_object.prop` is being used as a function, and `data.prop` is being used as a string. Must the types of property names be the same across completely
        Message 3 of 12 , Jun 20, 2011
        • 0 Attachment
          No, `other_object.prop` is being used as a function, and `data.prop` is being used as a string. Must the types of property names be the same across completely different objects?

          --- In jslint_com@yahoogroups.com, Douglas Crockford <douglas@...> wrote:
          >
          > The property .prop is being used as both a string and a function.
          >
          >
          >
          > On 6/20/2011 2:34 AM, Jordan wrote:
          > > The following code:
          > >
          > > /*global other_object */
          > > /*global other_object */
          > > var obj = {
          > > callback: function (data) {
          > > if (typeof (data.prop) === 'string') {
          > > other_object.prop('Property: ' + data.prop);
          > > }
          > > }
          > > };
          > >
          > > produces the following error:
          > >
          > > Problem at line 5 character 46: Type confusion: string and function.
          > > other_object.prop('Property: ' + data.prop);
          > >
          > > This only seems to happen when "other_object" is listed as a global and defined in another file - when I define it in the same file there is no error. In addition, if I replace the `if` condition with "if (typeof (data.prop) === 'string') {" (ie, even more explicitly defining the type of `data.prop`) it produces the same JSLint error.
          > >
          > > - Jordan
          > >
          > >
          > >
          > > ------------------------------------
          > >
          > > Yahoo! Groups Links
          > >
          > >
          > >
          > >
          >
        • Douglas Crockford
          ... Do you think that is not confusing?
          Message 4 of 12 , Jun 20, 2011
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "Jordan" <ljharb@...> wrote:
            > > The property .prop is being used as both a string and a function.

            > No, `other_object.prop` is being used as a function, and `data.prop` is being used as a string. Must the types of property names be the same across completely different objects?


            Do you think that is not confusing?
          • Erik Eckhardt
            The two `prop`s are properties of different objects. I don t see why that is type confusion because property names do not have meaning beyond the object they
            Message 5 of 12 , Jun 20, 2011
            • 0 Attachment
              The two `prop`s are properties of different objects. I don't see why that is
              type confusion because property names do not have meaning beyond the object
              they belong in. They're not variables scoped to a function, so identifying
              them as a clash within a function is to promote them to a bigger something
              they simply aren't.

              var obj1 = {
              ______doit : true
              ___},
              obj2 = {
              ______doit : function() {//do something}
              }
              if (obj1.doit) {
              obj2.doit();
              }

              There is no conflict here in usage. Names of variables are not arguably
              immutable in meaning when they don't share the same scope. It's legitimate
              for doit to be a boolean in one case signifying whether to do it, and to be
              a function in another to actually carry it out. What if these come from
              different libraries that we don't have control over? Our code is clean as a
              whistle, both libraries properly scoped themselves as modules, there
              shouldn't be a problem.

              If you would not throw an type confusion error in the following code
              (because the two `doit`s don't belong to the same scope) then I don't see
              how it should apply across object boundaries either:

              var doit = true;
              function x() {
              ___this.doit = function () {//do something};
              ___doit = false;
              ___this.doit();
              }


              On Mon, Jun 20, 2011 at 6:00 AM, Douglas Crockford <douglas@...>wrote:

              > **
              >
              >
              > The property .prop is being used as both a string and a function.
              >
              >
              > On 6/20/2011 2:34 AM, Jordan wrote:
              > > The following code:
              > >
              > > /*global other_object */
              > > /*global other_object */
              > > var obj = {
              > > callback: function (data) {
              > > if (typeof (data.prop) === 'string') {
              > > other_object.prop('Property: ' + data.prop);
              > > }
              > > }
              > > };
              > >
              > > produces the following error:
              > >
              > > Problem at line 5 character 46: Type confusion: string and function.
              > > other_object.prop('Property: ' + data.prop);
              > >
              > > This only seems to happen when "other_object" is listed as a global and
              > defined in another file - when I define it in the same file there is no
              > error. In addition, if I replace the `if` condition with "if (typeof
              > (data.prop) === 'string') {" (ie, even more explicitly defining the type of
              > `data.prop`) it produces the same JSLint error.
              > >
              > > - Jordan
              > >
              > >
              > >
              > > ------------------------------------
              > >
              > > Yahoo! Groups Links
              > >
              > >
              > >
              > >
              >
              >
              >


              [Non-text portions of this message have been removed]
            • Jordan
              What is the point of object-oriented code and scope if I must independently maintain an application-wide lookup table of property names to variable types? In
              Message 6 of 12 , Jun 20, 2011
              • 0 Attachment
                What is the point of object-oriented code and scope if I must independently maintain an application-wide lookup table of property names to variable types?

                In addition, let's say they are both functions that do two entirely different things. That wouldn't set off a type confusion error, but is that not potentially more confusing?

                I think it should be a basic requirement that the consumer of code understands that properties on different objects could easily have different characteristics, and I do not find it confusing to require them to familiarize themselves with it.

                --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
                >
                > --- In jslint_com@yahoogroups.com, "Jordan" <ljharb@> wrote:
                > > > The property .prop is being used as both a string and a function.
                >
                > > No, `other_object.prop` is being used as a function, and `data.prop` is being used as a string. Must the types of property names be the same across completely different objects?
                >
                >
                > Do you think that is not confusing?
                >
              • Douglas Crockford
                ... The point is to make your code easier to understand. But if you think that is not important, then turn on the Tolerate type confusion option.
                Message 7 of 12 , Jun 20, 2011
                • 0 Attachment
                  --- In jslint_com@yahoogroups.com, "Jordan" <ljharb@...> wrote:

                  > What is the point of object-oriented code and scope if I must independently maintain an application-wide lookup table of property names to variable types?


                  The point is to make your code easier to understand. But if you think that is not important, then turn on the Tolerate type confusion option.
                • John Hawkinson
                  Douglas Crockford wrote on Mon, 20 Jun 2011 ... I think it is normal -- different authors may touch different pieces of code. Sometimes
                  Message 8 of 12 , Jun 20, 2011
                  • 0 Attachment
                    Douglas Crockford <douglas@...> wrote on Mon, 20 Jun 2011
                    at 16:57:46 -0000 in <itnu6a+6cen@...>:

                    >> Must the types of property names be the same across completely
                    >> different objects?
                    >
                    > Do you think that is not confusing?

                    I think it is normal -- different authors may touch different pieces of code.
                    Sometimes different files may be JSLinted together. DIfferent APIs
                    may make different uses of same-named properties -- in fact, a single API
                    may do so. Writing code to such an API should not generate JSLint
                    warnings. It does not seem like one should have to give up type checking
                    to achieve this.

                    The scoping argument also seems correct -- JSLint should not pretend that
                    the namespace of properties is global across objects.

                    --jhawk@...
                    John Hawkinson
                  • livefree75
                    ... of code. ... API ... checking ... that ... Not only that, but specifying arguments for two different Javascript libraries may cause this error. E.g.:
                    Message 9 of 12 , Jun 27, 2011
                    • 0 Attachment
                      --- In jslint_com@yahoogroups.com, John Hawkinson <jhawk@...> wrote:
                      >
                      > Douglas Crockford douglas@... wrote on Mon, 20 Jun 2011
                      > at 16:57:46 -0000 in <itnu6a+6cen@...:
                      >
                      > >> Must the types of property names be the same across completely
                      > >> different objects?
                      > >
                      > > Do you think that is not confusing?
                      >
                      > I think it is normal -- different authors may touch different pieces
                      of code.
                      > Sometimes different files may be JSLinted together. DIfferent APIs
                      > may make different uses of same-named properties -- in fact, a single
                      API
                      > may do so. Writing code to such an API should not generate JSLint
                      > warnings. It does not seem like one should have to give up type
                      checking
                      > to achieve this.
                      >
                      > The scoping argument also seems correct -- JSLint should not pretend
                      that
                      > the namespace of properties is global across objects.
                      >
                      > --jhawk@...
                      > John Hawkinson
                      >

                      Not only that, but specifying arguments for two different Javascript
                      libraries may cause this error. E.g.:

                      Using the $.fn.width() function in jQuery base library:

                      $("#my_el").width(100);

                      Using the width option in jQuery Flexigrid

                      var colModel = [
                      { display: 'UID' , name : 'uid' , width : 61 },
                      { display: 'Name', name : 'name', width : 150 }
                      ];

                      causes this error to be thrown - complaining about the "width" property
                      being both a function and a number.

                      I can't help what names the libraries require me to use. In this case,
                      they don't even conflict, and I don't find it confusing.

                      I don't want to give up type-checking to avoid the a warning that
                      shouldn't exist in this case.



                      [Non-text portions of this message have been removed]
                    • bartman1c
                      so, it will be easier to understand write different arrays that hold redirects to random generated names of stuff in objects, sweet. Turning off jslint would
                      Message 10 of 12 , Jul 2, 2011
                      • 0 Attachment
                        so, it will be easier to understand write different arrays that hold redirects to random generated names of stuff in objects, sweet. Turning off jslint would work better o.o
                      • s_lubowsky
                        Dont want to beat a dead horse, but I agree completely. While I would like to keep type confusion checking on for cases where it really is useful, I find that
                        Message 11 of 12 , Jul 7, 2011
                        • 0 Attachment
                          Dont want to beat a dead horse, but I agree completely. While I would like to keep type confusion checking on for cases where it really is useful, I find that the new restrictive policy not allowing same named identifiers on different objects to have different types much too restrictive. I simply turn it off for all my files.

                          Perhaps there could be two levels of type confusion, one that honored object scoping as a more restrictive one that didnt?

                          Thanks
                          Stephen


                          --- In jslint_com@yahoogroups.com, Erik Eckhardt <erik@...> wrote:
                          >
                          > The two `prop`s are properties of different objects. I don't see why that is
                          > type confusion because property names do not have meaning beyond the object
                          > they belong in. They're not variables scoped to a function, so identifying
                          > them as a clash within a function is to promote them to a bigger something
                          > they simply aren't.
                          >
                          > var obj1 = {
                          > ______doit : true
                          > ___},
                          > obj2 = {
                          > ______doit : function() {//do something}
                          > }
                          > if (obj1.doit) {
                          > obj2.doit();
                          > }
                          >
                          > There is no conflict here in usage. Names of variables are not arguably
                          > immutable in meaning when they don't share the same scope. It's legitimate
                          > for doit to be a boolean in one case signifying whether to do it, and to be
                          > a function in another to actually carry it out. What if these come from
                          > different libraries that we don't have control over? Our code is clean as a
                          > whistle, both libraries properly scoped themselves as modules, there
                          > shouldn't be a problem.
                          >
                          > If you would not throw an type confusion error in the following code
                          > (because the two `doit`s don't belong to the same scope) then I don't see
                          > how it should apply across object boundaries either:
                          >
                          > var doit = true;
                          > function x() {
                          > ___this.doit = function () {//do something};
                          > ___doit = false;
                          > ___this.doit();
                          > }
                          >
                          >
                          > On Mon, Jun 20, 2011 at 6:00 AM, Douglas Crockford <douglas@...>wrote:
                          >
                          > > **
                          > >
                          > >
                          > > The property .prop is being used as both a string and a function.
                          > >
                          > >
                          > > On 6/20/2011 2:34 AM, Jordan wrote:
                          > > > The following code:
                          > > >
                          > > > /*global other_object */
                          > > > /*global other_object */
                          > > > var obj = {
                          > > > callback: function (data) {
                          > > > if (typeof (data.prop) === 'string') {
                          > > > other_object.prop('Property: ' + data.prop);
                          > > > }
                          > > > }
                          > > > };
                          > > >
                          > > > produces the following error:
                          > > >
                          > > > Problem at line 5 character 46: Type confusion: string and function.
                          > > > other_object.prop('Property: ' + data.prop);
                          > > >
                          > > > This only seems to happen when "other_object" is listed as a global and
                          > > defined in another file - when I define it in the same file there is no
                          > > error. In addition, if I replace the `if` condition with "if (typeof
                          > > (data.prop) === 'string') {" (ie, even more explicitly defining the type of
                          > > `data.prop`) it produces the same JSLint error.
                          > > >
                          > > > - Jordan
                          > > >
                          > > >
                          > > >
                          > > > ------------------------------------
                          > > >
                          > > > Yahoo! Groups Links
                          > > >
                          > > >
                          > > >
                          > > >
                          > >
                          > >
                          > >
                          >
                          >
                          > [Non-text portions of this message have been removed]
                          >
                        • Jérôme DESPATIS
                          +1 For now, I run JSLint with tolerate type confusion for all my .js, and then a second turn with type confusion and see file by file if I consider this type
                          Message 12 of 12 , Jul 7, 2011
                          • 0 Attachment
                            +1

                            For now, I run JSLint with tolerate type confusion for all my .js, and
                            then a second turn with type confusion and see file by file if I
                            consider this type confusion really an error or not...

                            Le 07/07/2011 15:32, s_lubowsky a écrit :
                            >
                            > Dont want to beat a dead horse, but I agree completely. While I would
                            > like to keep type confusion checking on for cases where it really is
                            > useful, I find that the new restrictive policy not allowing same named
                            > identifiers on different objects to have different types much too
                            > restrictive. I simply turn it off for all my files.
                            >
                            > Perhaps there could be two levels of type confusion, one that honored
                            > object scoping as a more restrictive one that didnt?
                            >
                            > Thanks
                            > Stephen
                            >
                            > --- In jslint_com@yahoogroups.com
                            > <mailto:jslint_com%40yahoogroups.com>, Erik Eckhardt <erik@...> wrote:
                            > >
                            > > The two `prop`s are properties of different objects. I don't see why
                            > that is
                            > > type confusion because property names do not have meaning beyond the
                            > object
                            > > they belong in. They're not variables scoped to a function, so
                            > identifying
                            > > them as a clash within a function is to promote them to a bigger
                            > something
                            > > they simply aren't.
                            > >
                            > > var obj1 = {
                            > > ______doit : true
                            > > ___},
                            > > obj2 = {
                            > > ______doit : function() {//do something}
                            > > }
                            > > if (obj1.doit) {
                            > > obj2.doit();
                            > > }
                            > >
                            > > There is no conflict here in usage. Names of variables are not arguably
                            > > immutable in meaning when they don't share the same scope. It's
                            > legitimate
                            > > for doit to be a boolean in one case signifying whether to do it,
                            > and to be
                            > > a function in another to actually carry it out. What if these come from
                            > > different libraries that we don't have control over? Our code is
                            > clean as a
                            > > whistle, both libraries properly scoped themselves as modules, there
                            > > shouldn't be a problem.
                            > >
                            > > If you would not throw an type confusion error in the following code
                            > > (because the two `doit`s don't belong to the same scope) then I
                            > don't see
                            > > how it should apply across object boundaries either:
                            > >
                            > > var doit = true;
                            > > function x() {
                            > > ___this.doit = function () {//do something};
                            > > ___doit = false;
                            > > ___this.doit();
                            > > }
                            > >
                            > >
                            > > On Mon, Jun 20, 2011 at 6:00 AM, Douglas Crockford <douglas@...>wrote:
                            > >
                            > > > **
                            > > >
                            > > >
                            > > > The property .prop is being used as both a string and a function.
                            > > >
                            > > >
                            > > > On 6/20/2011 2:34 AM, Jordan wrote:
                            > > > > The following code:
                            > > > >
                            > > > > /*global other_object */
                            > > > > /*global other_object */
                            > > > > var obj = {
                            > > > > callback: function (data) {
                            > > > > if (typeof (data.prop) === 'string') {
                            > > > > other_object.prop('Property: ' + data.prop);
                            > > > > }
                            > > > > }
                            > > > > };
                            > > > >
                            > > > > produces the following error:
                            > > > >
                            > > > > Problem at line 5 character 46: Type confusion: string and function.
                            > > > > other_object.prop('Property: ' + data.prop);
                            > > > >
                            > > > > This only seems to happen when "other_object" is listed as a
                            > global and
                            > > > defined in another file - when I define it in the same file there
                            > is no
                            > > > error. In addition, if I replace the `if` condition with "if (typeof
                            > > > (data.prop) === 'string') {" (ie, even more explicitly defining
                            > the type of
                            > > > `data.prop`) it produces the same JSLint error.
                            > > > >
                            > > > > - Jordan
                            > > > >
                            > > > >
                            > > > >
                            > > > > ------------------------------------
                            > > > >
                            > > > > Yahoo! Groups Links
                            > > > >
                            > > > >
                            > > > >
                            > > > >
                            > > >
                            > > >
                            > > >
                            > >
                            > >
                            > > [Non-text portions of this message have been removed]
                            > >
                            >
                            >


                            --
                            Jérôme Despatis
                            jerome@...



                            [Non-text portions of this message have been removed]
                          Your message has been successfully submitted and would be delivered to recipients shortly.