2 Replies Latest reply on Apr 16, 2017 12:15 AM by akiva_atwood

    Prototype issue

    akiva_atwood

      I'm playing with adding FP functionality -- but prototype isn't working for me.

       

      A code sample - I get an "a.indexOf is not a function" error. Anyone see why I'm getting that error? It works fine in a javascript editor.

       

      main();

       

      function main(){

      var a=[4,9,16];

      alert(a);

      b=a.indexOf(9);

      alert(b);

      }

       

       

      Array.prototype.indexOf = function (elem, fromIndex){

          fromIndex = Number(fromIndex) || 0;

          var len = this.length;

          if (fromIndex < 0){

              fromIndex += len;

          }

          for(var i=fromIndex;i<this.length;i++){

              if(this[i] == elem){

                  return i;

              }

          }

          return -1;

      }

        • 1. Re: Prototype issue
          Laubender Adobe Community Professional & MVP

          Hi,

          declare your prototype inside of main() before using it.

           

          This is working:

           

          main();
          
          function main()
          {
             
              Array.prototype.indexOf = function (elem, fromIndex)
              {
          
                  fromIndex = Number(fromIndex) || 0;
                  var len = this.length;
                  if (fromIndex < 0)
                  {
                      fromIndex += len;
                  }
                  for(var i=fromIndex;i<this.length;i++)
                  {
                      if(this[i] == elem)
                      {
                          return i;
                      }
                  }
                  return -1;
          
              };
          
              var a=[4,9,16];
              alert(a);
              b=a.indexOf(9);
              alert(b);
          
          };
          

           

          Or declare your prototype before everything else:

           

          Array.prototype.indexOf = function (elem, fromIndex)
          {
          
              fromIndex = Number(fromIndex) || 0;
              var len = this.length;
              if (fromIndex < 0)
              {
                  fromIndex += len;
              }
              for(var i=fromIndex;i<this.length;i++)
              {
                  if(this[i] == elem)
                  {
                      return i;
                  }
              }
              return -1;
          
          };
          
          main();
          
          function main()
          {
          
              var a=[4,9,16];
              alert(a);
              b=a.indexOf(9);
              alert(b);
          };
          

           

          But e.g. this is not working:

           

          main();
          
          function main()
          {
          
              var a=[4,9,16];
              alert(a);
              b=a.indexOf(9);
              alert(b);
              
              Array.prototype.indexOf = function (elem, fromIndex)
              {
          
                  fromIndex = Number(fromIndex) || 0;
                  var len = this.length;
                  if (fromIndex < 0)
                  {
                      fromIndex += len;
                  }
                  for(var i=fromIndex;i<this.length;i++)
                  {
                      if(this[i] == elem)
                      {
                          return i;
                      }
                  }
                  return -1;
          
              };
          
          };
          

           

          ExtendScript is a bit picky on prototype.

           

          BTW: You can use the forum's Advanced Editing feature to format your text as code.

          >> Insert > Syntax Highlighting > javascript

           

           

          Regards,
          Uwe

          • 2. Re: Prototype issue
            akiva_atwood Level 1

            Thanks. I figured scope would take care of that...