Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create listeners with javascript
    primarykey
    data
    text
    <p>I need to create a main class <code>animal</code>, then have two sub classes inherit properties from <code>animal</code> which are <code>cat</code> and <code>dog</code>, then I was to make multiple objects (multiple cat's and dog's) inherit from the class <code>cat</code> and <code>dog</code> (I read it's not really a class but it behaves as a class).</p> <p>The multiple objects (cat1, mycat, tomcat, dog1, mydog, topdog) need to be able to talk and listen to each other. When talking I would have them just alert or print a text line. What I am having trouble with is the listening part. I want the individual myDog to print out "dog heard cat" when myCat makes a sound. This is the code I have so far.</p> <pre><code>var animal = function (name, sound, listen) { var f = 0; this.name = name; // this.prop means prop is public var. this.sound = sound; this.listen = listen; this.makesound = function () { alert(this.sound); } this.listen = function () { alert(this.name + "heard that"); } return this; }; /*--------------- inheritance -----------------------------------*/ var cat = function () { this.name = name; this.sound = 'meow'; return this; }; cat.prototype = new animal(); function dog(name) { dog.sound = 'woof'; dog.name = name; return this; }; dog.prototype = new animal(); /*-------- different kinda dogs/cats ----------------------------*/ var myCat = new cat(); var myDog = new dog(); var tomCat = new cat(); var bigDog = new dog(); /*-----------------------*/ </code></pre> <p>I think I'm supposed to use an <code>if</code> statement, I heard I can use like a <code>if (callback) callback{}</code> but my attempts to implement something like that are failing. I'm BRAND new to javascript only been learning it last couple days. Any help or assistance would be really appreciated.</p>
    singulars
    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.
 

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