English 中文(简体)
a. 单体内功能
原标题:access a function inside a javascript object

我在物体内很难获得功能。

我的发言就是这样。

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.

即便是这样做的正确途径,也非常能发现,当他们身处仅用于ole目的的物体内时,如何利用功能。 我的想法

CAREERS.init.initialize(); 

would work but it doesn t.

任何帮助或建议都将受到高度赞赏。

增 编

吉尔吉斯

最佳回答

。 除非你做一些事情,否则不能从<条码>这一条外查阅。

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

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>>例两例引入。 页: 1 页: 1 有<代码>Careers 构造单页载荷排印。 (每当谷歌火灾时,则可能无法操作)

我删除了其中一条电话: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
    }

})();

......除非你只想到one<>>>>> 例(而且你确信不会改变),否则根本就没有要求施工人员:

(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; 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. The call that the interpreter makes to that function is our single object.

If you re ever going to need more than one Career instance, then great, definitely go the constructor function route. 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).


<>Off-topic:Strongly,建议宣布CAREERS。 您的代码目前为rel=“nofollow”>。

问题回答

两度界定物品的目的是什么?

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




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.