• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Prototype issue

Explorer ,
Apr 15, 2017 Apr 15, 2017

Copy link to clipboard

Copied

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 == elem){

            return i;

        }

    }

    return -1;

}

TOPICS
Scripting

Views

639

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 15, 2017 Apr 15, 2017

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 == elem)

            {

                return i;

            }

        }

        return -1;

    };

    var a=[4,9,16];

    al

...

Votes

Translate

Translate
Community Expert ,
Apr 15, 2017 Apr 15, 2017

Copy link to clipboard

Copied

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 == 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 == 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 == 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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 16, 2017 Apr 16, 2017

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines