分享

访问javascript对象内的函数

 印度阿三17 2019-06-30

我在访问对象内部的函数时遇到问题.

我的设置就是这样..

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();
    });
}

使用此设置,初始化不会运行,但如果我从初始化函数中取出我的谷歌地图变量/函数然后它的工作,但我的理解(来自谷歌文档)是初始化应始终是包含谷歌地图变量的函数/功能等

即使这是正确的方法,如果只是出于控制台中的调试目的而在对象方法中找到如何访问函数将是很好的.我想

CAREERS.init.initialize(); 

会工作,但事实并非如此.

任何帮助或建议将不胜感激.

谢谢

贾尔斯

解决方法:

初始化函数对于你放在this.init上的函数是真正私有的.除非你做一些让它可以访问的东西,否则无法从外部访问this.init函数.

但我不认为你需要额外的间接层:

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();
    });
}

但是,另外,您的代码正在尝试两次初始化Careers实例.你有谷歌的加载回调调用jQuery的ready函数,然后调用你的CareersInit函数调用CAREERS.init.但是,您还可以让Careers constructure安排单独的页面加载回调. (那个可能运行也可能不运行,这取决于Google何时触发setOnLoadCallback回调.)

我将摆脱其中一个对init的调用.

在对另一个答案的评论中,你已经说过你想知道“最好”的方法是什么.我必须更多地了解你在做什么,但我可能会这样做:

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

})();

…除非你只想要一个实例(并且你确定它不会改变),否则根本就没有构造函数的调用:

(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";
    }
})();

在那里,功能范围是单个实例;不需要单独的构造函数,playing games with this等.请注意,我们没有创建任何全局变量,someData的范围是匿名函数.解释器对该函数的调用是我们的单个对象.

如果您需要多个Career实例,那么很棒,绝对是构造函数路径.但如果没有,如果你使用你已经拥有的对象(调用函数的执行上下文),那么就会有更少的麻烦.

偏离主题:强烈建议您声明您的CAREERS变量.使用现在的代码,你将成为The Horror Of Implicit Globals的牺牲品.

来源:https://www./content-1-284401.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多