开源 Web 应用程序中良好 JavaScript 代码的示例

发布于 2024-11-11 06:45:28 字数 1536 浏览 1 评论 0 原文

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

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

发布评论

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

评论(8

清醇 2024-11-18 06:45:28

我总是向任何对此感兴趣的人推荐的是:坚持你的团队所做的事情。如果他们使用驼峰命名法作为方法,您就使用它。如果他们使用 snake_case 作为变量,你就这么做。如果您的团队更喜欢空格而不是制表符;使用它们。

永远不要进入一个标准化的风格改变事物的稳定团队,因为它看起来更好,除非它引起严重的问题。

如果您没有在团队中工作并且对使用编码风格感兴趣;然后使用您最常使用的库的样式

组织方面,闭包是最好的..但对我来说,不知怎的,感觉就像我正在阅读 JAVA 而不是 javascript。去算算吧。

What I always recommend to anyone who is interested in this kind of thing is: STICK TO WHAT YOUR TEAM DOES. If they use camelCase for methods, you use it. If they use snake_case for variables, you do it. If your team prefers spaces over tabs; use them.

Never go into a stablished team with standardized style changing things because it looks better unless it's causing heavy problems.

If you're not working on a team and you're interested on using a coding style; then use the style of the libraries you use the most.

Organization wise, Closure is the best.. but to me somehow it feels like I'm reading JAVA instead of javascript. Go figure.

灵芸 2024-11-18 06:45:28

是的。有一些 JavaScript 专家写了很多关于如何编写 JavaScript、关于使用 JavaScript 进行基于原型的 OOP,甚至关于应该如何缩进和命名变量的文章。

但是,如果您正在寻找 JavaScript 的大型实现来作为示例进行研究,我会寻找 HTML5 游戏实现。实际上可以保证您会找到一个足够大、编写良好且未被缩小的示例。

Yep. There are a few JavaScript gurus that have written alot about how to write JavaScript, about prototype based OOP with JavaScript, even about how indenting and naming variables should be done.

However, if you are looking for a large implementation of JavaScript to study as an example, I would look for HTML5 game implementations. It's practically guaranteed that you will find a large enough, well written example that is not minified.

つ低調成傷 2024-11-18 06:45:28

如果您对 JavaScript 标准感兴趣,我会查看 commonJS。他们对于如何完成 JavaScript 有很多好主意。

BravoJS 是浏览器的一个很好的模块实现。

至于示例,评论中提到了 jQuery 的源代码。 jQuery 做得很好,但我也会查看 Narwhal JS 以获取应该如何完成事情的示例。

这是一本很好的免费设计模式书籍,我发现它很有帮助Essential JavaScript And jQuery Design Patterns

你不会找到一种解决方案来解决你的问题,这就是 JavaScript 的设计方式。我建议尝试一下。我发现道格拉斯·克罗克福德有很多很棒的想法,但这并不意味着你必须严格遵循他的想法。

If you are interested in JavaScript standards I would check out commonJS. They have a lot of good ideas about how JavaScript should be done.

BravoJS is a good module implementation for the browser.

As for examples jQuery's source code was mentioned in the comments. jQuery does a good job but it is I would also check out Narwhal JS for examples of how things should be done.

Here is a good free design patterns book that I found helpful Essential JavaScript And jQuery Design Patterns.

You wont find one solution to your problem and that is how JavaScript is designed. I would recommended experimenting. I find that Douglas Crockford has a lot of great ideas but that does not mean you have to follow him to the letter.

深海里的那抹蓝 2024-11-18 06:45:28

很好的问题。我找不到一个编写良好的面向对象开源应用程序的示例。 Tiny MCE 很一般,但我认为写得不好: http://www.tinymce.com/< /a>

然而,我在工作中编写了干净的、分解良好的面向对象的 JavaScript。它是专有的,所以我不能分享它,但我可以解释什么对我有用,以学习如何做到这一点:

1)阅读mozilla javascript面向对象编程教程。他们对 javascript 继承的解释正是 google 闭包所使用的。就我个人而言,我认为 Crockford 所说的伪经典最容易阅读和维护,因为我知道的其他 5 种编程语言中有 4 种使用类(java、c#、python 和 php.perl 是这里的奇怪东西,也没有类)。

https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript

2) 阅读Stoyan Stefanov 的《面向对象 Javascript》一书。

3) 采用现有的过程式 javascript 代码库并将其重构为对象。使用 Robert C. Martin 的“整洁代码”中的技巧,因为它们适用于任何编程语言。

4) 构建代码,使其具有许多不同的文件,类似于在带有类的语言中使用类的方式。

5) 通过在顶层创建所有对象并将它们提供给依赖于它们的对象,在没有 IOC 容器的情况下实现依赖注入。

还有更多内容,但希望这是一个有用的开始。

这是我认为在 javascript 中实现继承的正确方法。这是来自谷歌闭包库:

goog.inherits = function(childCtor, parentCtor) {
  /** @constructor */
  function tempCtor() {};
  tempCtor.prototype = parentCtor.prototype;
  childCtor.superClass_ = parentCtor.prototype;
  childCtor.prototype = new tempCtor();
  childCtor.prototype.constructor = childCtor;
};

Great question. I couldn't find one example of a well written object oriented open source application. Tiny MCE was so-so, but I wouldn't consider it well written: http://www.tinymce.com/

However, I have written clean, well factored object oriented javascript at work. It's proprietary so I can't share it, but I can explain what worked for me to learn how to do that:

1) Read the mozilla javascript object oriented programming tutorial. Their explanation of javascript inheritance is exactly what google closure uses. Personally I think what Crockford calls pseudo classical is easiest to read and maintain since 4 of the 5 other programming languages I know use classes (java, c#, python, and php. perl's the oddball here with no classes either).

https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript

2) Read the book 'Object Oriented Javascript' by Stoyan Stefanov.

3) Take an existing procedural javascript code base and refactor it to objects. Use the tips in 'Clean Code' by Robert C. Martin as they apply to any programming language.

4) Structure your code so it has many different files similar to how you'd use classes in a language with classes.

5) Implement dependency injection without an IOC container by creating all your objects at a top level and feeding them down into the objects that depend on them.

There's a lot more, but hopefully this is a helpful start.

Here is what I feel is the correct way to implement inheritance in javascript. this is from the google closure library:

goog.inherits = function(childCtor, parentCtor) {
  /** @constructor */
  function tempCtor() {};
  tempCtor.prototype = parentCtor.prototype;
  childCtor.superClass_ = parentCtor.prototype;
  childCtor.prototype = new tempCtor();
  childCtor.prototype.constructor = childCtor;
};
留蓝 2024-11-18 06:45:28

巧合的是,今天 SlashDot 上有一个 评论,评论者称其“保留了作为 JavaScript 程序员终极参考资源的桂冠”。有 1,100 页。

是的,这不是您正在寻找的示例应用程序,但这本书确实有很多关于最佳实践的示例和建议。

Coincidentally, today on SlashDot there is a review of the 6th edition of Javascript: The Definitive Guide, which the reviewer there says "retains its crown as the ultimate reference resource for JavaScript programmers." It's 1,100 pages.

Yes, this isn't the sample app you were seeking, but the book does have a lot of examples and advice about best practices.

懒猫 2024-11-18 06:45:28

有多种方法可以学习如何编写优秀的 JS 代码。

你可以看书。关于 JS 代码的组织和常见模式(包括继承)的最好的一本是 Stoyan Stefanov 的《JavaScript 模式》。

另一个好的学习方法是浏览其他开发人员的优秀代码并使用它们。从代码组织和模式使用的角度来看,我见过的最好的库是 Google Closure Library。它由 Google 在 RIA 内部使用,就像 Gmail Google Docs 一样。

There are several ways to learn how to write good JS code.

You can read books. The best one about organization of JS code and about common patterns including inheritance is JavaScript Patterns by Stoyan Stefanov.

Another good way to learn is just look through the excellent code of other developers and using it. The best library I've seen from the point of code organization and using of patterns is Google Closure Library. It is used internally by Google in the RIA like Gmail Google Docs.

审判长 2024-11-18 06:45:28

irc 中的一位好心人推荐了这本电子书,我发现它非常有帮助。

学习 JavaScript 设计模式

Addy Osmani 所著的一本书

http://addyosmani.com/resources/essentialjsdesignpatterns/book/< /a>

A kind person in irc suggested this eBook and I found it verry helpful.

Learning JavaScript Design Patterns

A book by Addy Osmani

http://addyosmani.com/resources/essentialjsdesignpatterns/book/

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