javascript 和 jquery 中的可访问性和代码组织问题

发布于 2024-08-01 12:55:04 字数 453 浏览 9 评论 0原文

我的程序中的代码顺序遇到了问题。 现在我在 document.ready(function() { } ); 中有一些东西,还有一些东西在它之外。 每当我移动任何东西(以组织或改进我的代码)时,我最终都会破坏一些东西,我猜测是因为声明的顺序或访问级别(即 document.ready(function {});< 之外的东西) /code> 无法访问其中的某些内容。

任何人都知道应该将

所有内容放置在 document.ready(function() {}); 中吗?
是否有任何理由在 document.ready(function() {}); 之外添加任何内容?
代码是否在 document.ready(function() {});< 内/code> 外部代码无法访问?

I'm having troubles with the order of my code in my program. right now I have some things with in document.ready(function() { } ); and some things out side of it. Whenever I move anything (to organize or improve my code) I end up breaking something, I'm guessing because of either order of declarations or access levels (ie. something outside of document.ready(function {}); cannot access something inside of it.

Anyone have any insight as to where things should be located javascript wise?

Should everything be within document.ready(function() {});?
Is there any reason to have anything outside of document.ready(function() {});?
Is the code within document.ready(function() {}); inaccessible by outer code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白云悠悠 2024-08-08 12:55:04

1. 一切都应该在范围之内
document.ready(function() {});?

不,我认为 document.ready 函数必须只是初始化事物、分配事件处理程序等。

2. 有什么理由可以拥有
任何之外的东西
document.ready(function() {});?

代码重用和更好的代码组织。

3. 代码是否在
文档.ready(function() {});
外部代码无法访问?

是的,在 document.ready 上创建的变量和对象无法从外部范围访问。

1 . Should everything be within
document.ready(function() {});?

No, I think that the document.ready function must be only to initialize things, assign event handlers and so on.

2 . Is there any reason to have
anything outside of
document.ready(function() {});?

Code reutilization, and better code organization.

3 . Is the code within
document.ready(function() {});
inaccessible by outer code?

Yes, the variables and objects created on the document.ready are inaccessible from the outer scope.

偷得浮生 2024-08-08 12:55:04

一切都应该在 document.ready(function() {}); 内吗?

是和不是。 在大型 JavaScript 应用程序中,我通过此事件处理程序将主控制器初始化到全局范围。 然而,有些代码不需要等待 DOM 准备好,特别是:不依赖 DOM 的代码。 我认为这非常简单。 例如,我在此事件处理程序之外声明配置对象类、函数等。

是否有任何理由在 document.ready(function() {}); 之外添加任何内容?

当然,原因已在上面提到过。 主要是,不需要 DOM 交互的代码不应该等待 DOM 加载,特别是如果它可以与 DOM 加载异步执行(例如:函数定义、配置对象等)。

此外,不将所有代码包含在一个事件处理程序中可以使事情更有条理,允许您模块化代码,使用正确的设计模式等。

代码是否在 document.ready(function() {}); 中? 外部代码无法访问?

再说一次,是和不是。 如果您使用 var 将其声明为本地,那么它是肯定的,外部作用域无法访问它,因为它对于事件处理程序来说是本地的; 否则,它位于全局范围内并且可以被外部范围访问。 这是一个示例(托管于此处:http://jsbin.com/uriqe

JavaScript

var foo = function() {
  alert(global);
  return false;
}

$(document).ready(function() {
  global = "you can see me!?";
  alert("global is initiated");
});

有意使用HTML

<body>
  <p><a href="#" onclick="foo()">click me</a></p>
</body>

onclick 而不是 $(document).ready() 中的不显眼的方法事件附件来避免有关 < code>foo 通过闭包属性可以访问 global

编辑:我想我在上一句中已经说得很清楚了,但是onclick是故意使用的,以避免全局范围和闭包属性之间的混淆,但是我并不提倡使用 onlick 当然,这是一种不好的做法,您不应该使用它。

Should everything be within document.ready(function() {});?

Yes and no. In large scale JavaScript applications, I initialize my main controllers into the global scope from this event handler. However, there is code that doesn't need to wait for the DOM to be ready, specifically: code that doesn't rely on the DOM. I think that's pretty straight forward. For example, I declare configuration objects classes, functions, etc outside of this event handler.

Is there any reason to have anything outside of document.ready(function() {});?

Sure, for the reason's touched on above. Mainly, code that doesn't require DOM interaction shouldn't wait for the DOM to load, especially if it can execute asynchronously to the DOM loading (e.g.: function definitions, configuration objects, etc).

Also, not including all of your code in one event handler keeps things more organized, allows you to modularize code, use proper design patterns, etc.

Is the code within document.ready(function() {}); inaccessible by outer code?

Again, yes and no. If you declare it as local with var then it is yes, it is inaccessible by the outer scope as it is local to the event handler; otherwise, it is in the global scope and is accessible to the outer scope. Here's an example (hosted here: http://jsbin.com/uriqe)

JavaScript

var foo = function() {
  alert(global);
  return false;
}

$(document).ready(function() {
  global = "you can see me!?";
  alert("global is initiated");
});

HTML

<body>
  <p><a href="#" onclick="foo()">click me</a></p>
</body>

onclick rather than unobtrusive method event attachment in $(document).ready() is used intentionally to avoid any questions/arguments about foo having access to global via the closure property.

Edit: I thought I made it clear in the previous sentence, but onclick is used intentionally to avoid confusion between global scope and the closure property, but I'm not advocating the use of onlick. Of course it's a bad practice and you shouldn't be using it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文