为什么附加到窗口[编辑] [英] why attach to window [edited]

查看:83
本文介绍了为什么附加到窗口[编辑]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看qunit的代码.

I was looking over the code for qunit.

我的问题是,为什么要通过属性将qunit对象附加到窗口对象.

My question is why would you want to attach the qunit object via property to window object.

这是文件的链接.看第11行.

Here is the link to the file. Look at line 11.

如果我看一下使用Firebug进行的单元测试,则可以看到它是window的属性.

If I look at a unit test run using firebug you can see it is a property of window.

另外:在特定名称空间中声明内容的最佳做法是否有特定参考?

[edit] Additional: Is there a specific reference for best practice for declaring things in specific namespaces?

推荐答案

所有全局对象(函数,变量等)只是window的子级,它是默认上下文.

All global objects (functions, variables, etc) are just children of window, it's the default context.

例如:window.jQuerywindow.$

这样想起来可能会更容易...您还会将它们放在哪里?当您执行此常规操作时,最好(或至少最简单)将其放置在默认位置.如果您要执行许多功能,对象等复杂的事情,最好将它们放在名称空间或对象中.例如,jQuery的所有代码都在jQuery下,而不是像window.ajax那样乱码在DOM的根目录中,而是在jQuery.ajax中.

It may be easier to think of it this way...where else would you put them? When you're doing something this general, best (or at least easiest) to stick them in the default place. If you're doing something complex with lots of functions, objects, etc...best to put them in a namespace or within an object. For example all of jQuery's code is under jQuery, not littered in the root of the DOM like window.ajax, instead it's jQuery.ajax.

这很整洁,但是当您处理一些项目时可能会过大,但是最好在这种情况下确保它们是唯一的... qunit可以这样做,方法是在其对象前加上qunit-

This is much neater, but perhaps overkill when you're dealing with a few items, but it's a good idea to make sure they are unique if this is the case...which qunit does, by prefixing their objects with qunit-

这篇关于为什么附加到窗口[编辑]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆