JavaScript中代码组织中常见的最佳做法 [英] Commonly accepted best practices around code organization in JavaScript

查看:126
本文介绍了JavaScript中代码组织中常见的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



世界你有没有保持这种有组织的?




  • 将所有的处理程序放在一个位置,并为所有的事件写入函数? li>
  • 创建函数/类来包装所有的功能?

  • 写得像疯狂一样,希望它能最好的发挥作用?

  • 放弃并获得新的职业生涯?



我提到jQuery,但它一般都是JavaScript代码。我发现,随着线上的线条开始堆积,它变得更难管理脚本文件或找到你要找的东西。我发现最大的问题是有很多方法可以做同样的事情,很难知道目前普遍接受的最佳做法是哪一种。



有没有关于保持您的 .js 文件与您的其他应用程序一样漂亮和整洁的最佳方法的一般建议?还是这只是IDE的问题?有没有更好的选择?






编辑



这个问题是为了更多的代码组织而不是文件组织。有一些非常好的合并文件或分割内容的例子。



我的问题是:目前普遍接受的最佳实践方法是组织实际代码是什么?您的方式是什么,甚至是推荐的方式与页面元素进行交互,并创建不会彼此冲突的可重复使用的代码?



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

解决方案

如果javascript已经建立了名称空间,那么很好,但是我发现组织的东西像Dustin Diaz这样描述了这里帮助我很多

  var DED =(function(){

var private_var;

function private_method()
{
// do stuff here
}

return {
method_1:function()
{
// do stuff here
},
method_2:function()
{
//在这里填写
}
};
})();

我将不同的命名空间和有时单独的类放在单独的文件中。通常我从一个文件开始,作为一个类或命名空间足够大以保证它,我把它分离成自己的文件。使用工具将所有文件合并到一起也是一个很好的想法。


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

How in the world do you keep this organized?

  • Put all your handlers in one spot and write functions for all the events?
  • Create function/classes to wrap all your functionality?
  • Write like crazy and just hope it works out for the best?
  • Give up and get a new career?

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.

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?


EDIT

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.

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?

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?

解决方案

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.

这篇关于JavaScript中代码组织中常见的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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