Skip to main content
Avoyt
Participant
August 17, 2021
Question

Array.prototype functions undefined in After Effects scripts?

  • August 17, 2021
  • 4 replies
  • 1430 views

So, a script I was developing was working fine yesterday, but today, it seems like my Array built-in object is broken. I tried restarting After Effects to reset the environment, but it's still broken. So far, I've identified that "map", "includes", and "split" are broken, though "shift" is fine. I don't have any custom startup scripts.

 

I've attached a simple .txt file that demonstrates this error for me, though I'm guessing it would work normally. Just rename it to .jsx and run it yourself.

 

OS: MacOS Big Sur v11.4

AE: v18.4.0 build 41

This topic has been closed for replies.

4 replies

Participant
October 16, 2024

same issue here in 2024.

resetting preferences did not solve it but
Edit -> Purge -> All Memory And Disk Cache ... did resolve it

in case this does not resolve it, you can manually write the array prototype functions in case they are missing:

// Polyfill for .map() if it doesn't exist
if (!Array.prototype.map) {
    Array.prototype.map = function(callback, thisArg) {
        var T, A, k;
        if (this == null) {
            throw new TypeError(" this is null or not defined");
        }
        var O = Object(this);
        var len = O.length >>> 0;
        if (typeof callback !== "function") {
            throw new TypeError(callback + " is not a function");
        }
        if (arguments.length > 1) {
            T = thisArg;
        }
        A = new Array(len);
        k = 0;
        while (k < len) {
            var kValue, mappedValue;
            if (k in O) {
                kValue = O[k];
                mappedValue = callback.call(T, kValue, k, O);
                A[k] = mappedValue;
            }
            k++;
        }
        return A;
    };
}

// Polyfill for .includes() if it doesn't exist
if (!Array.prototype.includes) {
    Array.prototype.includes = function(searchElement, fromIndex) {
        var O = Object(this);
        var len = parseInt(O.length, 10) || 0;
        if (len === 0) {
            return false;
        }
        var n = parseInt(fromIndex, 10) || 0;
        var k;
        if (n >= 0) {
            k = n;
        } else {
            k = len + n;
            if (k < 0) {
                k = 0;
            }
        }
        while (k < len) {
            if (O[k] === searchElement) {
                return true;
            }
            k++;
        }
        return false;
    };
}
 
Mathias Moehl
Community Expert
Community Expert
August 18, 2021

The worst difference I found so far is actually this one:

true || true && false

evaluates to "false" in After Effects ExtendScript, but to "true" in modern JavaScript.

Because After Effects reads it as

(true || true) && false

whereas modern JavaScript reads it as

true || (true && false)

 

So when writing logic statements, make sure to always set parenthesis to avoid trouble. And don't use minifiers like uglify.js which remove these parenthesis.

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Mathias Moehl
Community Expert
Community Expert
August 18, 2021

In general, you cannot expect Ae scripting to support all features of modern JavaScript.

After Effects scripting is using ExtendScript and ExtendScript implements the JavaScript language according to the ECMA-262 specification. This is a very old variant of JavaScript and it does not include features like Array.map, yet.

 

 

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Avoyt
AvoytAuthor
Participant
August 17, 2021

EDIT:

Whoops, "split" is a string function, it won't work anyway, ahahah.

Still, "map" and "includes" are both broken for me.