-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.
Daniel Price
  • 1,132
  • 8
  • 18
  • 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? – Daniel Price Apr 17 '14 at 10:49
  • 1
    possible duplicate of [Creating functions dynamically in JS](http://stackoverflow.com/questions/20129236/creating-functions-dynamically-in-js) (Found by simply googling for `dynamically create javascript function from code` ...) – CBroe Apr 17 '14 at 10:50
  • I was searching for methods, sorry. – Daniel Price Apr 17 '14 at 10:51

2 Answers2

1

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

S1cK94
  • 43
  • 7
  • I'm trying to refrain from using eval, thank you for the suggestion though. – Daniel Price Apr 17 '14 at 10:39
  • @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()`). – jfriend00 Apr 17 '14 at 10:49
  • 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. – CBroe Apr 17 '14 at 10:49
  • I'm sure eval would be a lot slower to run than actual functions, as javascript has to evaluate the code before executing it. – Daniel Price Apr 17 '14 at 10:54
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

Community
  • 1
  • 1
Daniel Price
  • 1,132
  • 8
  • 18