普遍接受的周围code组织的最佳做法在JavaScript中 [英] Commonly accepted best practices around code organization in JavaScript

查看:116
本文介绍了普遍接受的周围code组织的最佳做法在JavaScript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于JavaScript框架像jQuery使客户端的Web应用程序更丰富,功能更强大,我已经开始注意到一个问题...

As JavaScript frameworks like jQuery make client side web applications richer and more functional, I've started to notice one problem...

在世界上你是怎么保持这个组织的?


  • 把所有的处理程序在一个地方,写功能的所有事件?

  • 创建函数/类包装您的所有功能?

  • 写像疯了似的,只是希望它的作品出来最好?

  • 放弃,并得到一个新的职业生涯?

我提到的jQuery,但它真的任何JavaScript code一般。我发现,为对线线开始堆积,它就会更难管理脚本文件或找到你在找什么。很可能,我发现最大的propblems是,有这么多的方式做同样的事情,很难知道哪一个是当前普遍接受的最佳做法。

I mention jQuery, but it's really any JavaScript code in general. I'm finding that as lines upon lines begin to pile up, it gets harder to manage the script files or find what you are looking for. Quite possibly the biggest propblems I've found is there are so many ways to do the same thing, it's hard to know which one is the current commonly accepted best practice.

有没有的最佳途径的任何一般性建议,让您的的.js 文件作为漂亮整洁作为应用程序的其他部分?或者这只是IDE的问题?有没有更好的选择了吗?

Are there any general recommendations on the best way to keep your .js files as nice and neat as the rest of your application? Or is this just a matter of IDE? Is there a better option out there?

修改

这个问题的用意是更多关于code的组织,而不是文件组织。目前已经有合并文件或拆分内容的一些很好的例子。

This question was intended to be more about code organization and not file organization. There has been some really good examples of merging files or splitting content around.

我的问题是:什么是当前普遍接受的最佳实践的方式来组织你的实际code?什么是你的方式,甚至是推荐的方式与页面元素交互并创建可重复使用的code不相互冲突?

My question is: what is the current commonly accepted best practice way to organize your actual code? What is your way, or even a recommended way to interact with page elements and create reuseable code that doesn't conflict with each other?

有人列出的命名空间这是一个好主意。什么是一些其他的方法,更具体的处理页面上的元素,并保持code有组织整齐?

Some people have listed namespaces which is a good idea. What are some other ways, more specifically dealing with elements on the page and keeping the code organized and neat?

推荐答案

这将是更好的很多,如果JavaScript从一开始建的命名空间,但我发现,整理东西像达斯汀·迪亚兹介绍的这里帮助了我很多。

It would be a lot nicer if javascript had namespaces built in, but I find that organizing things like Dustin Diaz describes here helps me a lot.

var DED = (function() {

    var private_var;

    function private_method()
    {
        // do stuff here
    }

    return {
        method_1 : function()
            {
                // do stuff here
            },
        method_2 : function()
            {
                // do stuff here
            }
    };
})();

我把不同的命名空间,有时个别班级在不同的文件。通常我开始一个文件,并作为一个阶级或命名空间得到足够大,以保证它,我分离出来到自己的文件。使用工具的生产是一个很好的想法,以及给你所有的文件结合起来。

I put different "namespaces" and sometimes individual classes in separate files. Usually I start with one file and as a class or namespace gets big enough to warrant it, I separate it out into its own file. Using a tool to combine all you files for production is an excellent idea as well.

这篇关于普遍接受的周围code组织的最佳做法在JavaScript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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