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

Re: Type inconsistency: array and string

Expand Messages
  • Douglas Crockford
    ... value = value ? String(value.charAt(1) / 100) : 1 ;
    Message 1 of 6 , Jun 12, 2011
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "spence.randall@..." <randall@...> wrote:
      >
      > When entering the following code:
      >
      > var getStyle = function (element, property) {
      > 'use strict';
      > var value = element.currentStyle[property];
      > if (property === 'opacity') {
      > value = (element.style.filter || element.currentStyle.filter).match(/alpha\s*\(\s*opacity\s*=\s*([\d.]+)\)/i);
      > value = String(value ? value[1] / 100 : 1);
      > } else if (isNaN(parseFloat(value))) {
      > if (/^(height|width)$/i.test(property) && element.currentStyle.display !== 'none') {
      > value = element['offset' + property.charAt(0).toUpperCase() + property.slice(1)] + 'px';
      > }
      > }
      > return element.style[property] || value;
      > };
      >
      > JSLint produces the following error:
      >
      > Error:
      >
      > Problem at line 9 character 94: Type inconsistency: array and string.
      >
      > value = element['offset' + property.charAt(0).toUpperCase() + pro...


      value = value ? String(value.charAt(1) / 100) : '1';
    • spence.randall@ymail.com
      Thanks for the reply, although that solved the type inconsistency error in JSLint, it actually caused the code to error in the browser. Since value was
      Message 2 of 6 , Jun 12, 2011
      • 0 Attachment
        Thanks for the reply, although that solved the type inconsistency error in JSLint, it actually caused the code to error in the browser. Since value was assigned the results of a regular expression, charAt threw an error in IE. My solution was to simply create another variable to hold the regular expression result rather than reusing value over again.

        I really am starting to appreciate the new type consistency option for reasons such as this. I think this is a great addition to JSLint.

        -Randall

        --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
        >
        > --- In jslint_com@yahoogroups.com, "spence.randall@" <randall@> wrote:
        > >
        > > When entering the following code:
        > >
        > > var getStyle = function (element, property) {
        > > 'use strict';
        > > var value = element.currentStyle[property];
        > > if (property === 'opacity') {
        > > value = (element.style.filter || element.currentStyle.filter).match(/alpha\s*\(\s*opacity\s*=\s*([\d.]+)\)/i);
        > > value = String(value ? value[1] / 100 : 1);
        > > } else if (isNaN(parseFloat(value))) {
        > > if (/^(height|width)$/i.test(property) && element.currentStyle.display !== 'none') {
        > > value = element['offset' + property.charAt(0).toUpperCase() + property.slice(1)] + 'px';
        > > }
        > > }
        > > return element.style[property] || value;
        > > };
        > >
        > > JSLint produces the following error:
        > >
        > > Error:
        > >
        > > Problem at line 9 character 94: Type inconsistency: array and string.
        > >
        > > value = element['offset' + property.charAt(0).toUpperCase() + pro...
        >
        >
        > value = value ? String(value.charAt(1) / 100) : '1';
        >
      • Erik Eckhardt
        I see what you mean. Yet it seems to me that an intermediate expression shouldn t be considered as a type inconsistency problem because it was never assigned
        Message 3 of 6 , Jun 12, 2011
        • 0 Attachment
          I see what you mean. Yet it seems to me that an intermediate expression
          shouldn't
          be considered as a type inconsistency problem because it was never assigned
          to a variable. This code
          passes jslint:

          var x,
          ____v = String(x ? x[0] : 1);
          v = 'a';

          so why shouldn't his?

          On Sun, Jun 12, 2011 at 9:25 AM, Douglas Crockford <douglas@...>wrote:

          >
          >
          > --- In jslint_com@yahoogroups.com, "spence.randall@..." <randall@...>
          > wrote:
          > >
          > > When entering the following code:
          > >
          > > var getStyle = function (element, property) {
          > > 'use strict';
          > > var value = element.currentStyle[property];
          > > if (property === 'opacity') {
          > > value = (element.style.filter ||
          > element.currentStyle.filter).match(/alpha\s*\(\s*opacity\s*=\s*([\d.]+)\)/i);
          > > value = String(value ? value[1] / 100 : 1);
          > > } else if (isNaN(parseFloat(value))) {
          > > if (/^(height|width)$/i.test(property) && element.currentStyle.display
          > !== 'none') {
          > > value = element['offset' + property.charAt(0).toUpperCase() +
          > property.slice(1)] + 'px';
          > > }
          > > }
          > > return element.style[property] || value;
          > > };
          > >
          > > JSLint produces the following error:
          > >
          > > Error:
          > >
          > > Problem at line 9 character 94: Type inconsistency: array and string.
          > >
          > > value = element['offset' + property.charAt(0).toUpperCase() + pro...
          >
          > value = value ? String(value.charAt(1) / 100) : '1';
          >
          >
          >


          [Non-text portions of this message have been removed]
        • Douglas Crockford
          ... Is x a number or an array?
          Message 4 of 6 , Jun 14, 2011
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, Erik Eckhardt <erik@...> wrote:
            >
            > I see what you mean. Yet it seems to me that an intermediate expression
            > shouldn't
            > be considered as a type inconsistency problem because it was never assigned
            > to a variable. This code
            > passes jslint:
            >
            > var x,
            > ____v = String(x ? x[0] : 1);
            > v = 'a';
            >
            > so why shouldn't his?

            Is x a number or an array?
          • Erik Eckhardt
            Haha, well that makes it clear, doesn t it? Thank you. ... [Non-text portions of this message have been removed]
            Message 5 of 6 , Jun 14, 2011
            • 0 Attachment
              Haha, well that makes it clear, doesn't it?

              Thank you.

              On Tue, Jun 14, 2011 at 5:39 AM, Douglas Crockford <douglas@...>wrote:

              >
              >
              > --- In jslint_com@yahoogroups.com, Erik Eckhardt <erik@...> wrote:
              > >
              > > I see what you mean. Yet it seems to me that an intermediate expression
              > > shouldn't
              > > be considered as a type inconsistency problem because it was never
              > assigned
              > > to a variable. This code
              > > passes jslint:
              > >
              > > var x,
              > > ____v = String(x ? x[0] : 1);
              > > v = 'a';
              > >
              > > so why shouldn't his?
              >
              > Is x a number or an array?
              >
              >
              >


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