Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is how I would do it... I'd set those who are listening. <code>listening</code> is an optional parameter when you create a new object. You can also change those who are listening after creation using <code>setListening</code>. <code>listening</code> in this case will just be an array of objects that have a <code>name</code> property.</p> <p>Also, I fixed some issues, you either didn't have "name" or weren't setting it in any case.</p> <p><a href="http://jsfiddle.net/n9xCM/" rel="nofollow">http://jsfiddle.net/n9xCM/</a></p> <pre><code>var animal = function (name, sound, listening) { var f = 0; this.name = name; // this.prop means prop is public var. this.sound = sound; this.listening = listening; this.makesound = function () { alert(this.sound); this.listen(); } this.setListening = function (listening) { this.listening = listening; } this.listen = function () { console.log("in listen ("+this.listening.length+")"); for (var i = 0; i &lt; this.listening.length; i++) { alert(this.listening[i].name + " heard that"); } } return this; }; /*--------------- inheritance -----------------------------------*/ var cat = function (name, listening) { this.name = name; this.listening = listening; this.sound = 'meow'; return this; }; cat.prototype = new animal(); function dog(name, listening) { this.sound = 'woof'; this.listening = listening; this.name = name; return this; }; dog.prototype = new animal(); /*-------- different kinda dogs/cats ----------------------------*/ var myDog = new dog("mydog"); var myCat = new cat("mycat", [myDog]); var bigDog = new dog("buster"); var tomCat = new cat("tommy", [bigDog]); myCat.makesound(); //meow - mydog heard that bigDog.setListening([myDog, myCat, tomCat]); bigDog.makesound(); //woof - mydog/mycat/tommy heard that /*-----------------------*/ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload