Is it possible to copy functions and variables from one object to another?
Let say I have a DOM element with standard functions plus some custom event handlers. Can I somehow add more functions, which are members of other element, into it? The names of functions can be anything.
What about constructor like
Code:
({
...
// functions and variables
...
})
Can I somehow add those functions and variables into an existing DOM element?
Basically, I have a DOM element and constructor above as a string. I need to add the functions and variables into the DOM element at runtime. How can I do that?
As I understand it you want to extend an existing element adding user defined properties. You wish to copy these user defined properties from some object where they are defined. Is this correct?
1) The structure isn't a constructor, it's an initialiser, or literal (hence my confusion). So much for terminology.
2) I assume that the data must be held in a string - some JSON affair.
There was a nested quote problem with your string (now corrected).
You will have to parse the string. Using eval is the standard approach. It's already wrapped in (..). That's good.
PHP Code:
var elem = document.getElementById('p1');
var cons = '({ testFunk: function () { alert("Hello!");}})';
// You may want a try / catch here
cons = eval(cons)
Yeah, I could if I knew the content of cons in advance. But I'm setting the value of cons at runtime and it can be for instance:
Code:
var cons = "({ testFunk: function () { alert("Hello!");} "
+ " testFunk2: function () { alert("Hello too!");} })";
So, I get the string in that format. The question is can I utilize the constructor format somehow or do I need the parse the string?
I'm not sure if I make myself clear... The point is that I read the string in runtime. It's in constructor format but can contain any functions and variables with any names.
You are not
I still don't know what it is you're trying to acheive.
It's in constructor format but can contain any functions and variables with any names.
What do you mean by constructor format ant 'what' is in it? Try to explain you problem in plain english - no programming lingo - just give a describtion of the initial problem.
So the problem is creating dynamic functions? Why not use the Function constructor for this and avoid the use of eval.
Code:
// Object with functions created at runtime
var cons = new Object();
// Create a new function runtime
// First create function body
var fBody = 'alert('+someText+');';
// Create function and add to object
cons.testFunc = new Function(fBody);
// Copy object functions to element
var elm = document.getElementById('p1');
for(var p in cons)
elm[p] = cons[p];
Thanks guys, it works! I'm using the sample Banana A. proposed in post #9.
Originally Posted by Dok
So the problem is creating dynamic functions? Why not use the Function constructor for this and avoid the use of eval.
Because I get the functions as a string. That is, I don't create the strings by myself, but just reading them to the application as they are. All I know they are in the format described above.
Bookmarks