-1

I've recently been working on a nice little JavaScript game engine that works a lot like Game Maker, but lets people create basic JavaScript games within a browser. Every instance of every object will have it's own preset methods, which the runner will iterate through and execute. I'm trying to find a way to let the user / creator dynamically edit any of the methods source code. When I say 'preset methods', I mean blank methods stored under specific preset names within the objects / object instances. Here's a basic example:

var newObject = object_add("object_name"); // Adds a new object 'blueprint' and returns the reference.

The function object_add(); creates a JavaScript object, and adds a number of preset methods to it, such as:

  • create
  • destroy
  • step
  • draw

.. and many more

Each of these methods will have no code in them to start with. I need to let the creator dynamically change any of the methods source code. I could simply overwrite the variable that points towards the method, with a new method, but how can you set method's source code using a string?

I know that something like:

newObject.create = function(){textbox.innerHTML};

definitely wouldn't work. Any ideas?

Many thanks,

  • Dan.
3
  • I've been looking at closures, but don't really understand how they work. If i did something like: function changeSource(object,method,newcode){ object[method]=function(){newcode}; } would newcode become the actual source within that function, or not? Commented Apr 17, 2014 at 10:49
  • 1
    possible duplicate of Creating functions dynamically in JS (Found by simply googling for dynamically create javascript function from code ...) Commented Apr 17, 2014 at 10:50
  • I was searching for methods, sorry. Commented Apr 17, 2014 at 10:51

2 Answers 2

1

Looks like you want to use eval function, but it's generally a bad idea.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm trying to refrain from using eval, thank you for the suggestion though.
@DanielPrice - don't know how you dynamically set the source of a method from a user generated string without using eval() or manually creating a script tag with the source in it (which is not really any different than using eval()).
You want to dynamically create function objects from code in string form, so whether or not you are using eval or any other method does not really make a difference.
I'm sure eval would be a lot slower to run than actual functions, as javascript has to evaluate the code before executing it.
0

The answer was found at: Creating functions dynamically in JS

Here's the answer (copied from the other page).

Well, you could use Function, like in this example:

var f = new Function('name', 'return alert("hello, " + name + "!");');
f('erick');
//This way you're defining a new function with arguments and body and assigning it to a variable f. You could use a hashset and store many functions:

var fs = [];
var fs['f1'] = new Function('name', 'return alert("hello, " + name + "!");');
fs['f1']('erick');
//Loading xml depends if it is running on browser or server.

Thanks, @CBroe https://stackoverflow.com/users/1427878/cbroe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.