0

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.
Hachi
  • 536
  • 6
  • 17
  • 1
    Look up shallow and deep copying. More specifically in your case, deep copying. – jrd1 Jul 13 '13 at 06: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 06: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: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Jul 13 '13 at 06:24

4 Answers4

3

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);
}
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • +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 06:39
1

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.

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
  • 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 06:47
1

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.
Community
  • 1
  • 1
HMR
  • 37,593
  • 24
  • 91
  • 160
0

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335