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;
}
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
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
Copy link to clipboard
Copied
Thanks. I figured scope would take care of that...