简体   繁体   English

我如何学习javascript编码模式

[英]how can i learn javascript coding patterns

First what i mean by patterns.. Basically in Js there are multiple ways to do something but some ways of doing things offer greater benefits in terms of portability, performance, modularity, and extension. 首先,我所说的是模式。.基本上,在Js中,有多种方法可以完成某项工作,但是某些方法可以在可移植性,性能,模块化和扩展性方面带来更大的好处。 One of the patterns i like most are of jquery. 我最喜欢的模式之一是jquery。

But when writing my own code i feel urge to just keep on writing function after function...and i don't want to create an object just for the sake of organization. 但是在编写自己的代码时,我强烈希望继续编写一个又一个函数……而且我不想为了组织而创建对象。 There should be a reason like reusability for object to be created. 应该有诸如创建对象的可重用性之类的原因。

I want to learn patterns that make more use of closures, prototype, objects and chaining. 我想学习更多使用闭包,原型,对象和链接的模式。 So i can write better code. 这样我就可以编写更好的代码。

I know keeping code simple is best but when things are wide spread keeping code less intrusive and reusable is maybe more important. 我知道保持代码简单是最好的,但是当事情变得很广泛时,保持代码的侵入性和可重用性可能更重要。

Check this out: http://addyosmani.com/resources/essentialjsdesignpatterns/book/ 检查一下: http : //addyosmani.com/resources/essentialjsdesignpatterns/book/

This book was also pretty useful: JavaScript Patterns Build Better Applications with Coding and Design Patterns By Stoyan Stefanov Publisher: O'Reilly Media 这本书也非常有用:JavaScript模式通过编码和设计模式构建更好的应用程序作者:Stoyan Stefanov出版商:O'Reilly Media

This book really helped me getting started. 这本书确实帮助我入门。 Apart from that I suggest googling up, reading various articles,blogs, whatever you think is useful. 除此之外,我建议您谷歌搜索,阅读各种文章,博客,无论您认为有用什么。

Don't bother trying to learn every single aspect of JavaScript before you actually need it. 在您真正需要JavaScript之前,不要费心尝试学习JavaScript的每个方面。

If you suddenly find yourself passing masses of variables to your functions then you might find it easier to use objects, but objects are generally only useful if the data they encapsulate is related in some way. 如果突然发现自己将大量变量传递给函数,则可能会发现使用对象更容易,但是对象通常仅在它们封装的数据以某种方式相关时才有用。 That means since you are aware of objects then as soon as the need arises for them in your code, you'll realise that's the time to use them. 这意味着,既然您意识到了对象,然后在代码中对它们的需求就会增加,您将意识到是时候使用它们了。

Since objects properties and methods are so easily added to variables in JavaScript it's probably overkill to write out functions to construct objects/override variable prototypes etc. 由于对象属性和方法是如此容易地添加到JavaScript中的变量中,因此写出构造对象的函数/覆盖变量原型等可能是过大的杀伤力。

As a very broad generalization, in languages such as Perl and php, for most programs of less than 1000 lines objects are overkill. 作为一种非常广泛的概括,在诸如Perl和php之类的语言中,对于大多数少于1000行的程序来说,对象是过大的。

Play it by ear and take the easy way out every time. 耳边弹奏,每次轻松走出路。

I watched this Google talk a few weeks ago and was inspired to read Crockford's entire book, "JavaScript: The Good Parts". 几周前,我观看了这个 Google演讲,并受到启发阅读了Crockford的整本书“ JavaScript:The Good Parts”。 Watch the talk and I think you'll find it's exactly what you're looking for, full of best practices for using closures & prototype. 观看演讲,我想您会发现这正是您要寻找的东西,其中包含使用闭包和原型的最佳实践。 It's a little old and just covers core JavaScript, nothing about JQuery, ect. 它有点旧,仅涵盖核心JavaScript,与JQuery等无关。 but if that's what you're looking for, this is your book. 但是,如果这就是您想要的,那就是您的书。

http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742 http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM