Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript对象函数调用无法工作_Javascript_Oop - Fatal编程技术网

Javascript对象函数调用无法工作

Javascript对象函数调用无法工作,javascript,oop,Javascript,Oop,我正在开发HTML5音板,但我遇到了一点障碍 我正在尝试使用一个停止函数来同时处理所有播放的声音。不幸的是,当我从buttonpress调用此函数时,对象似乎没有停止函数。实际声音元素的代码如下所示: // Container to keep all sounds in one place. This is a Dictionary within a dictionary to be able to search by catagory. var sounds = {}; // Counter

我正在开发HTML5音板,但我遇到了一点障碍

我正在尝试使用一个停止函数来同时处理所有播放的声音。不幸的是,当我从buttonpress调用此函数时,对象似乎没有停止函数。实际声音元素的代码如下所示:

// Container to keep all sounds in one place. This is a Dictionary within a dictionary to be able to search by catagory.
var sounds = {};

// Counter to keep track of unique ID's
var lastID = 0;


// Base class for initializing Any class
var Base = function(methods){
    var base = function() {    
        this.initialize.apply(this, arguments);          
    };  

    for (var property in methods) { 
       base.prototype[property] = methods[property];
    }

    if (!base.prototype.initialize) base.prototype.initialize = function(){};      

    return base;    
};

//Complete class for the Sound object. Generates its own DIV's and HTML5 tags to play stuff.
var Sound = Base({ 
    // Init all the variables. 
    initialize: function(name, file, target='Sounds') {
        this.name = name;
        this.file = file
        this.button = null;
        this.audioelement;
        this.id = lastID + 1;
        this.target = target;
        lastID ++;


        // Check if the catagory is there, if not: create it with a placeholder object
        var catagory = sounds[this.target];
        if(catagory == null){
            sounds[this.target] = {99:null};

        }
        sounds[this.target][this.id] = this;

        // Call init function
        this.init();
    },

    play : function() {
        obj = this
        if(obj.audioelement.paused == true){
            obj.audioelement.play();    
        }else{
            obj.audioelement.pause();
            obj.audioelement.fastSeek(0);
        }

    },
    stop : function(){
        obj = this;
        obj.audioelement.pause();
    },
    init : function(){

        // Statement for JS class based shenanigans.
        obj = this

        // Create a button and add some text to it
        obj.button = document.createElement("BUTTON");
        obj.button.appendChild(document.createTextNode(obj.name));

        // Set ID's and names to keep track of this button
        obj.button.id = obj.id;
        obj.button.name = obj.target;

        // Get or create parent element. Used for catagory based display
        var el = getOrCreateElement(obj.target)
        el.appendChild(obj.button);

        // Create audio element and set appropriate settings
        obj.audioelement = document.createElement("AUDIO");
        obj.audioelement.src = obj.file;
        obj.audioelement.name
        obj.button.appendChild(obj.audioelement);

        // Add function to play/pause to button
        obj.button.onclick = buttonClicked;

});
function buttonClicked(){
    // Fetch sound from dicionary container using the name and id from the button [SET AT SOUND.INIT()]
    var sound = sounds[this.name][this.id];
    // Call the play function in [SOUND]
    sound.play();
}
对于stopall函数:

function stopAll(){
    // Scroll through the entire dictionary
    for ( var key in sounds){
        for ( var id in sounds[key]){
            // Check if the sound is not a placeholder
            if(id == 99){
                continue;
            }
            // Call stop function with fetched object.
            var sound = sounds[key][id];
            sound.stop();
        }
    }
}
奇怪的是,play函数似乎确实起作用,但stop函数却不起作用。它说这个对象没有那个特定的功能

任何想法都将受到欢迎

WM

for(对象中的var x)
在多个属性上循环,而不仅仅是方法,包括基本对象原型上的方法

如果您
console.log(id)在该内部循环中,您将看到额外的循环

尝试在内部for循环中添加以下内容:

if (typeof id !== 'function') {
    continue;
}
编辑:这不太正确,但我现在还不能确定它应该是什么。记忆中,它就快到了!继续玩它。

for(对象中的var x)
循环使用的属性不止是方法,包括基本对象原型上的方法

如果您
console.log(id)在该内部循环中,您将看到额外的循环

尝试在内部for循环中添加以下内容:

if (typeof id !== 'function') {
    continue;
}

编辑:这不太正确,但我现在还不能确定它应该是什么。记忆中,它就快到了!继续玩它。

如果需要帮助,您应该发布用于播放/停止的代码。实际功能都在那里。唯一缺少的是调用函数本身的逻辑。我将添加这样做的函数。您是否有任何错误消息?如果需要帮助,您应该发布用于播放/停止的代码。实际函数都在那里。唯一缺少的是调用函数本身的逻辑。我将添加执行此操作的函数。您是否有任何错误消息?这仅适用于非ES5环境,因此在非常旧的浏览器中是正确的。嗯,可能是7年前的事了数据只有在非ES5环境中才是如此,所以在非常旧的浏览器中也是如此。嗯,可能是7年前吧:D