1

I have something like this:

dog = function () {
  this.age;
  this.name;
  this.dog1 = new dog.shepherd();
}

dog.prototype = {
  bark : function () {
    alert( (this.getName()) + " is barking." );
  },

  [3]**getName** : function () {
    return this.name;
  }
}

dog.shepherd = function () {
  this.something;
}

dog.shepherd.prototype = function () {
  guard : function () {
        alert( ([2]**parent.getName()**) + " is barking." );
  }
}

window.onload = function () {
  var n = new dog();
  [1]**n.guard()**;
}

How can I use a function which is prototyped to the dog from dog.speherd prototype?

In other words, when [1] is executed, then [2] is written in pseudo-code, to call [3].

I know this code shouldn't work; it's just to help me explain what I want.

4
  • 1
    It's not really clear what it is that you want/expect to happen. What effect are you trying to achieve? Commented Nov 26, 2011 at 16:51
  • 1
    Christoph: Are you aware that dog.shepherd = function () { this.something; } has no impact on inheritance? You're just referencing one function on a property of another function. Commented Nov 26, 2011 at 16:54
  • Are you just trying to make a Shepherd subclass for dog or are you trying to have dogs have the shepherd methods? Commented Nov 26, 2011 at 16:58
  • I've been thinking about it. I wanted to reference one function to another, but I should use inheritance here as I think more deeply. Commented Nov 26, 2011 at 19:16

2 Answers 2

3

Your title says you want to use composition, but your question body implies inheritance. Here's how you would do this with composition, which is probably the better option. A shepherd "is not a" dog after all (nor vice versa) so inheritance probably isn't right here.

Also, you usually don't want to set the entire prototype of a function like you're doing; just add the functions you want. And constructor functions by convention start with a capital letter.

function Shepherd(mydog) { 
    this.dog = mydog;
}
Shepherd.prototype.guard = function () {
    alert( this.dog.getName() + " is barking." );
}

function Dog(age, name) {
  this.age = age;
  this.name = name;
}

Dog.prototype.getName = function () {
    return this.name;
}

Dog.prototype.bark = function () {
    alert(this.getName() + " is barking." );
}

window.onload = function () {
  var someDog = new Dog(4, "Ubu");
  var someShepherd = new Shepherd(someDog);      
  someShepherd.guard();
}

jsfiddle

Sign up to request clarification or add additional context in comments.

Comments

0

I don't know why you need such code, but you can try this:

dog = function () {
  this.age;
  this.name;
  this.dog1 = new dog.shepherd(this);

  var d = this.dog1;
  for (var i in dog.shepherd.prototype) {
    this[i] = function(){
       return this.dog1[i].apply(d, arguments);
    }
  }
}

dog.prototype = {
  bark : function () {
    alert( (this.getName()) + " is barking." );
  },

  getName : function () {
    return this.name;
  }
}

dog.shepherd = function (parent) {
  this.parent = parent;
  this.something;
}

dog.shepherd.prototype = {
  guard : function () {
        alert( (this.parent.getName()) + " is barking." );
  }
}

window.onload = function () {
  var n = new dog();
  n.guard();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.