www.webdeveloper.com
Results 1 to 15 of 15

Thread: Copying functions to another element

  1. #1
    Join Date
    Jan 2007
    Posts
    9

    Question Copying functions to another element

    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?
    Last edited by aulio; 07-25-2007 at 06:38 AM.

  2. #2
    Join Date
    Jun 2007
    Posts
    347
    In short, Yes. Probably. You may have to give a more specific example.

  3. #3
    Join Date
    Jan 2007
    Posts
    9
    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?

  4. #4
    Join Date
    Jun 2007
    Posts
    347
    Give a little example of what you have.

  5. #5
    Join Date
    Jan 2007
    Posts
    9
    For example, something like this:

    Code:
    var elem = document.getElementById('p1'); // Returns a HTML p element
    
    var cons = "({ testFunk: function () { alert("Hello!");}})";
    Now, I want to add content of cons to the elem. Do I have to parse the string or can I somehow utilize the constructor format it has?

  6. #6
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    You can just do
    Code:
    document.getElementById('p1').testFunk = function(){alert('Hello');};

  7. #7
    Join Date
    Jan 2007
    Posts
    9
    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:

    var cons = "({ testFunk: function () { alert("Hello!");} "
    })";

  8. #8
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    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?

  9. #9
    Join Date
    Jun 2007
    Posts
    347
    A couple of things first.

    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)

    for(var 
    p in cons)
        
    elem[p] = cons[p]; 

  10. #10
    Join Date
    Jan 2007
    Posts
    9
    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.

  11. #11
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    I'm not sure if I make myself clear
    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.

  12. #12
    Join Date
    Jun 2007
    Posts
    347
    (See my post #9 above)

    I understand what you are doing, but..

    The latest cons string is malformed.

    1) Nested quote problem again (get a decent code editor and you see this immediately).

    2) Missing comma between members.
    Last edited by Banana Ananda; 07-30-2007 at 07:20 AM.

  13. #13
    Join Date
    Jun 2007
    Posts
    347
    But I'm setting the value of cons at runtime
    In that case, I don't see the need to use a string. Is there some reason you aren't using a plain code ?

  14. #14
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    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];

  15. #15
    Join Date
    Jan 2007
    Posts
    9
    Thanks guys, it works! I'm using the sample Banana A. proposed in post #9.

    Quote 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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles