如何使用javascript创建监听器 [英] How to create listeners with javascript

查看:134
本文介绍了如何使用javascript创建监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个主类 animal ,然后有两个子类继承自 animal 的属性, code> cat 和 dog ,那么我要使多个对象(多个猫和狗的)继承自 cat dog (我读的不是一个类,而是一个类)。

I need to create a main class animal, then have two sub classes inherit properties from animal which are cat and dog, then I was to make multiple objects (multiple cat's and dog's) inherit from the class cat and dog (I read it's not really a class but it behaves as a class).

多个对象(cat1,mycat,tomcat,dog1,mydog,topdog)需要能够彼此通话和收听。当说话时,我会让他们只警告或打印文本行。我遇到的麻烦是听音部分。我希望个人myDog打印出狗听猫myCat发出声音。这是我到目前为止的代码。

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.

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();
/*-----------------------*/

我想我应该使用 if 语句,我听说我可以使用像
if(callback)callback {} 但我尝试实现这样的东西失败。我是BRAND新的javascript只是学习它最后几天。任何帮助或帮助将非常感激。

I think I'm supposed to use an if statement, I heard I can use like a if (callback) callback{} 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.

推荐答案

这是我会做的...我会设置那些谁正在听。 listening 是创建新对象时的可选参数。您还可以使用 setListening 更改创建后正在侦听的用户。 listen 在这种情况下只是一个具有 name 属性的对象数组。

This is how I would do it... I'd set those who are listening. listening is an optional parameter when you create a new object. You can also change those who are listening after creation using setListening. listening in this case will just be an array of objects that have a name property.

此外,我修复了一些问题,你没有名字或者没有设置它。

Also, I fixed some issues, you either didn't have "name" or weren't setting it in any case.

< a href =http://jsfiddle.net/n9xCM/ =nofollow> http://jsfiddle.net/n9xCM/

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 < 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

/*-----------------------*/

这篇关于如何使用javascript创建监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆