Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm sure this has been explained somewhere, but I have never found it so, here I go:

Let's say I have an object that contains functions move, attack, defend. How can I add them to a unit-object, without adding them one at a time, or adding a sub.object: units.commands = commands, rather: units = commands OR units.prototype = commands.

Another simple example:

    var one = function  () {
        this.a = 1;
    };
    var two = function  () {
        this.z = 1;
    };
    var jee = new one(),
    jee2 = new two();
    jee = jee2;
    console.log(jee); // Should have z AND a.
share|improve this question
1  
Look up shallow and deep copying. More specifically in your case, deep copying. –  jrd1 Jul 13 '13 at 6:23
3  
possible duplicate of How can I merge properties of two JavaScript objects dynamically? –  koala_dev Jul 13 '13 at 6:23
    
what you are currently doing is: (1) cluttering the global scope with variable jee2. (2) Overwriting the pointer to an object one with a pointer to an object two. –  Sumurai8 Jul 13 '13 at 6:23
1  
Are you looking to inherit from an object or inject functionality in an object? Here is an answer that will get you going with inheritance: stackoverflow.com/questions/16063394/… –  HMR Jul 13 '13 at 6:24
add comment

4 Answers

up vote 3 down vote accepted

You could make another function three that takes the values from the other two, like:

var Three = function(){
  one.call(this);
  two.call(this);
}

var three = new Three; //=> Three{a:1, z:1}

With underscore you could even compose the functions an call them altogether:

var Three = function(){
  _.compose(one, two).call(this);
}
share|improve this answer
    
+1 First solution will work nicely for OP to combine one and two. It would not copy any of one and two's prototype but the sample code the OP gave didn't use prototype. –  HMR Jul 13 '13 at 6:39
add comment

What you are searching for, is how to inherit the properties of one object on an other. This is not done by simply overwriting the pointer of one object to an other. I think this page can help you.

share|improve this answer
    
There might've been some problem for me, cause prototypes are a difficult concept for me, but they don't seem to help in this case much either. Is is possible to add multiple prototypes to one object. ie: one.__proto__ = two; one.__proto__ = three; ? Actually I think it could just be thought, that I'm trying to achieve multiple inheritance. So setting the prototype more than once. Almost every example in javascript always uses "sub-object" to add functionality / variable for the object ie: "one.something = something" and never "one += something", "one = something" etc. –  Hachi Jul 13 '13 at 6:47
add comment

Looking at the comments you could go a couple of ways.

  1. Merging the objects (sorry for using jQuery here but cloning an object isn't that easy and requires a whole lot of code) to merge objects you can use $.extend
  2. Inherit from an object if you're planning the object two to extend one see here
  3. Injecting functionality in an object. Don't have sample code for that at the moment.
share|improve this answer
add comment

You would have to loop over each property of one object and copy them one-by-one onto the other object.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.