How can I fix the error message "Scope.eval is not a function" ---

dhjdhjdhj's icon

I had a file called include.js that I found on the net that allows me to easily include other files in my js code. Works beautifully on Max 5 but fails on Max 6 with the error above.

There's a comment that eval has been disabled on Max 6 so I'm wondering if there's another way to get the same functionality.

Thanks,
D

dhjdhjdhj's icon

To answer my own question, I think the following works as an alternative for global stuff, which is all I need

Instead of the original line

scope.eval(mem);

I used the following instead

var myFunction = new Function(mem);
myFunction();

The only difference with this version is that it doesn't have the 'scope' context but I'm not sure how necessary that is.

Anyone know?

Jan M's icon

Couls you post you include.js? What exactly is it supposed to do
J.

dhjdhjdhj's icon

See this link. It's very handy.

hallluke.wordpress.com/2010/10/31/including-extra-javascript-files

Jan M's icon

Hi D,

as far as my knowledge goes the main difference between eval() and using an anonymous Function() constructor is the scope they have access too.
eval can access scopes outside itself, the Function constructor can't. (This is the reason why some say "eval is evil." and I guess the reason why it was removed... )

This means practically:

var myFunction = function() {
   var a = 1;
   eval("a=a+1");
   return a;
}

will return 2. While

var myFunction = function() {
   var a = 1;
   (new Function("a=a+1"));
   return a;
}

will return 1 (as the a inside the constructor is not in the same scope as the one outside.

As fas as I see it that means for the solution you mentioned that you can run the code inside the included file on startup/loading of your [js ] - object but you cannot execute any function that are inside. (At least i am not able to do so on a windows machine...).

A workaround to this problem I could think of is this one:
Alter the end of include.js to

var myFunction = new Function(mem);
return myFunction();

and use the include function inside your [js ] - object like this.

var myIncludedObject = include(this,"/Path/To/File.js");
function bang() {
   myIncludedObject.myMethod("something");
   myIncludedObject.myProperty;
}

I got this to work if your included JS-file has this sightly odd/not slick design pattern:

/Path/To/File.js :

return {
    myProperty: 2222,
    myMethod: function(MyVar) {
        post(MyVar);
    }
};

But maybe someone has a more elegant solution .....

Jan