简体   繁体   English

访问 javascript object 内的 function

[英]access a function inside a javascript object

I'm having trouble accessing a function inside an object.我在访问 object 内的 function 时遇到问题。

My setup is as such..我的设置是这样的..

google.setOnLoadCallback(function(){$(document).ready(CareersInit);});

function CareersInit()
{
     CAREERS = new Careers();
     CAREERS.init();
}

function Careers()
{
     this.init = function()
     {
         function initialize() 
         {
             //Usual google maps stuff here
         }
     }

    $('body').bind('onload', function() {
        initialize();
    });
}

With this setup, initialize doesn't run but if I take my google maps variables/functions out of the initialize function then it work's but my understanding (from google docs) is that initialize should always be a function that contains the google maps variables/functions etc.使用此设置,初始化不会运行,但如果我将我的谷歌地图变量/函数从初始化 function 中取出,那么它可以工作,但我的理解(来自谷歌文档)是初始化应该始终是包含谷歌地图变量/的 function功能等

Even if that is the right way to go it would be great to find out how to access functions when they are inside an objects method just for debug purposes in the console.即使这是 go 的正确方法,如果仅出于在控制台中进行调试的目的,了解如何在对象方法中访问函数也会很棒。 I thought我想

CAREERS.init.initialize(); 

would work but it doesn't.会工作,但它没有。

Any help or advice would be greatly appreciated.任何帮助或建议将不胜感激。

Thanks谢谢

Giles贾尔斯

The initialize function is truly private to the function you're putting on this.init . initialize function 是真正私有的 function 您正在使用this.init It cannot be accessed from outside that this.init function unless you do something to make it accessible. this.init不能从外部访问它,除非您采取措施使其可访问。

But I don't think you need that extra layer of indirection:但我认为您不需要额外的间接层:

google.setOnLoadCallback(function(){$(document).ready(CareersInit);});

function CareersInit()
{
     CAREERS = new Careers();
     CAREERS.init();
}

function Careers()
{
    var self = this;

    this.init = function()
    {
        //Usual google maps stuff here
    };

    $('body').bind('onload', function() {
        self.init();
    });
}

Separately, though, your code is trying to initialize the Careers instance twice.但是,另外,您的代码正在尝试两次初始化Careers实例。 You have Google's load callback calling jQuery's ready function which then calls your CareersInit function which calls CAREERS.init .你有谷歌的负载回调调用 jQuery 的ready function 然后调用你的CareersInit function 调用CAREERS.init But you also have the Careers constructure scheduling a separate page load callback.但是你Careers结构安排一个单独的页面加载回调。 (That one may or may not run, it depends on when Google fires the setOnLoadCallback callback.) (这可能会或可能不会运行,这取决于 Google 何时触发setOnLoadCallback回调。)

I'd get rid of one of those calls to init .我会摆脱那些对init的调用之一。

In a comment on another answer, you've said you want to know what the "best" way to do it is.在对另一个答案的评论中,您说过您想知道“最好”的方法是什么。 I'd have to know more about what you're doing, but I'd probably do it like this:我必须更多地了解你在做什么,但我可能会这样做:

(function() {
    // Our single Careers instance
    var CAREERS;

    // Ask Google to call us when ready
    google.setOnLoadCallback(function(){
        // Just in case Google is ready before the DOM is,
        // call our init via `ready` (this may just call
        // us immediately).
        $(document).ready(CareersInit);
    });

    // Initialize our single instance    
    function CareersInit()
    {
         CAREERS = new Careers();
         CAREERS.init();
    }

    // Constructor    
    function Careers()
    {
    }

    // Career's init function
    Careers.prototype.init = Careers_init;
    function Careers_init()
    {
        //Usual google maps stuff here
    }

})();

...except that if you're going to just have one instance (and you're sure that's not going to change), there's really no call for a constructor function at all: ...除了如果您只拥有一个实例(并且您确定这不会改变),那么根本不需要构造函数 function :

(function() {
    // Our data; the function *is* the single object
    var someData;

    // Ask Google to call us when ready
    google.setOnLoadCallback(function(){
        // Just in case Google is ready before the DOM is,
        // call our init via `ready` (this may just call
        // us immediately).
        $(document).ready(CareersInit);
    });

    // Initialize our single instance    
    function CareersInit()
    {
         someData = "some value";
    }
})();

There, the function scope is the single instance;在那里,function scope单个实例; no need for separate constructor functions, playing games with this , etc. Note that we're not creating any globals, someData is scoped to the anonymous function.不需要单独的构造函数,this玩游戏等。请注意,我们没有创建任何全局变量, someData的范围为匿名 function。 The call that the interpreter makes to that function is our single object.解释器对该 function 的调用我们的单个 object。

If you're ever going to need more than one Career instance, then great, definitely go the constructor function route.如果您需要多个Career实例,那么很好,绝对是 go 构造函数 function 路线。 But if not, there's a lot less fuss involved if you use the object you already have (the execution context of the call to the function).但如果没有,如果你使用你已经拥有的 object (函数调用的执行上下文),所涉及的问题就会少得多。


Off-topic : Strongly recommend declaring your CAREERS variable.题外话强烈建议声明您的CAREERS变量。 With your code as it is now, you're falling prey to The Horror Of Implicit Globals .使用你现在的代码,你正在成为隐式全局恐怖的牺牲品。

initialize is a private function inside init , which means it is inaccessible outside init . initializeinit内部的私有 function ,这意味着它在init外部不可访问。

What exactly is the purpose of defining things twice?定义事物两次的目的究竟是什么?

 this.init = function()
 {
         //Usual google maps stuff here
 }

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

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