English 中文(简体)
用(文件)打碎卷宗,但保持范围。
原标题:break js to files with $(document).ready() but keep scope

我要把我的javascript代码破成几卷。 其中每个部分都有需要在美元(文件)内填写的代码。 因此,在每个档案中,将开立新的(文件)。

我怎么能从档案中宣布的职能中提一下(在文件上)。 ready

If this is not possible, can you propose an alternative? Thank you.

Edit:我要澄清,我想避免利用全球范围。 我在利用指定职能作为活动处理者这一行文,但我确实能够看到如何这样做。

最佳回答

You really can t get away from declaring a global. Creating a single global isn t so bad, you can then namespace all your functions under it.

把它用在像主人这样的东西。 j 档案,以便你能够在此保持共同职能:

// name this something unique to your page/site/app
var MYAPP = {};

// now we can attach functions to it

MYAPP.funcA = function() { /* ... */ };
MYAPP.funcB = function() { /* ... */ };

然后,在你匿名的每一项职能中,你都可以接触MYAPP.funcA()、MYAPP.funcB()等。 你们还可以在传单上修改皇家警察部队,以增加职能、财产等。

In the end you have a single global (darn it!), but if you ve named it properly you are creating a global namespace where your app code can safely reside.

问题回答

You can make a local variable global with

window.globalname = localname;

职能是变量。

只要这些档案按顺序装上(即filea.js的功能在fileb.js之前装上。 打电话给他们,应当罚款。

In order to make sure files load their dependencies first, you could consider require.js or head.js

I ve had luck with the latter: http://headjs.com/





相关问题
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.

热门标签