简体   繁体   English

如何在另一个函数中访问对象函数

[英]How to access Object function in another function

In my buildEstat function , I build up my script element, and then I wait for it to load...then I call eSloaded(), which will create an Object on var contentStreamTag. 在我的buildEstat函数中 ,我建立了脚本元素,然后等待它加载...然后调用eSloaded(),它将在var contentStreamTag上创建一个对象

Object {} 
    - notifyPlayer: function()
    - post: function()
    - set: function()

My question is, in my bindEvents() function , I have some play/pause/mute functions. 我的问题是,在我的bindEvents()函数中 ,我有一些播放/暂停/静音功能。 In those play/pause/mute functions, I need to use some object functions inside the var contentStreamTag, such as the post/set function 在那些播放/暂停/静音功能中,我需要在var contentStreamTag内部使用一些对象功能,例如发布/设置功能

I am having a hard time figuring this out. 我很难解决这个问题。 Please let me know if my question is confusing or does not make sense. 如果我的问题令人困惑或没有道理,请告诉我。

     buildEstat: function() {

        var eS = document.createElement('script');
        eS.type = 'text/javascript';
        eS.async = true;
        eS.src = ('https:' === document.location.protocol ? 'https://' : 'http://') + 'prof.estat.com/js/mu-5.1.js';

        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(eS, s);

        if (eS.addEventListener) {
            eS.addEventListener('load', function() {
                eSloaded();
            }, false);
        } else {
            console.log('something not working')
        }

        function eSloaded() {
            var contentStreamTag = new eStatTag(confStreamingAnalytics);    
            // console.log(contentStreamTag);            
        }
    },

    bindEvents: function() {
        var self = this;

        this.buildEstat(function(){

        });

        this.dom.play.click(function() {
            $(this).css('display', 'none');
            $('.gp_pause').css('display', 'block');
            self.sound.play();

        });
        this.dom.pause.click(function() {
            $(this).css('display', 'none');
            $('.gp_play').css('display', 'block');
            self.sound.unload();
            self.sound.stop();

        });

        this.dom.mute.click(function() {
            self.sound.toggleMute();
            $(this).toggleClass('muted');
        });

        $('#goomplayer').addClass('animated bounceInUp');

    },

contentStreamTag is a var inside the eSloaded function. contentStreamTag是eSloaded函数内部的var。 Which means once that function has finished, that variable will no longer exist. 这意味着一旦该函数完成,该变量将不再存在。 If you wish for it to be more persistent, you will need to store it at a more global level, either as a global variable or maybe in localStorage. 如果希望它更具持久性,则需要将其存储在更全局的级别上,既可以作为全局变量,也可以存储在localStorage中。

Javascript variables are scoped in the function where it is defined. Javascript变量在定义它的函数中作用域。

To access the variable in a parent scope, you can move declaration to parent scope: 要访问父作用域中的变量,可以将声明移到父作用域中:

function parent(){
    var foo;
    function innerScope(){
        foo = 'bar';
    }
    innerScope();
    console.log(foo);
}

This adds a complexity to your code, because the variable will exist in the parent scope but will not be initialized until the innerScope is called. 这会增加代码的复杂性,因为该变量将存在于父作用域中,但在调用innerScope之前不会被初始化。 This can be addressed, for example turning innerScope into an object and foo a property of it. 可以解决这个问题,例如将innerScope变成一个对象,并将foo变成它的属性。 But exactly how to address this depends on more info on how your code works and how it's going to be used. 但是,确切的解决方法取决于您的代码如何工作以及如何使用的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM