简体   繁体   English

使用Javascript的“好部分”的最佳方式

[英]Best way to use Javascript's “good parts”

On Stackers' recommendation, I have been reading Crockford's excellent Javascript: The Good Parts . 在Stackers的推荐下,我一直在阅读Crockford的优秀Javascript:The Good Parts

It's a great book, but since so much of it is devoted to describing the best way to use Javascript's basic functionality, I'm not sure how I can put his advice into practice without duplicating the efforts of many other Javascript programmers. 这是一本很棒的书,但由于其中很多都致力于描述使用Javascript基本功能的最佳方式,我不确定如何在不重复许多其他Javascript程序员的努力的情况下将他的建议付诸实践。

Take this passage, for example: 以这段话为例,例如:

When you make a new object, you can select the object that should be its prototype. 创建新对象时,可以选择应该是其原型的对象。 The mechanism that Javascript provides to do this is messy and complex, but it can be significantly simplified. Javascript提供的机制是混乱和复杂的,但它可以大大简化。 We will add a create method to the Object function. 我们将向Object函数添加一个create方法。 The create method creates a new object that uses an old object as its prototype. create方法创建一个使用旧对象作为其原型的新对象。

 if (typeof Object.create !== 'function') { Object.create = function(o) { var F = function () {}; F.prototype = o; return new F(); } 

I could manually add this code to all my Javascript projects, but keeping track of everything would be a huge pain. 我可以手动将此代码添加到我的所有Javascript项目中,但跟踪所有内容将是一个巨大的痛苦。

Are there any libraries that implement The Good Part 's recommendations and thereby save me the trouble of having to keep track of them (/ physically type them all out)? 是否有任何库实现了Good Part的建议,从而省去了必须跟踪它们(/物理地将它们全部输出)的麻烦?

Prototype has many great features, including a Class helper that handles the details of JS "inheritance" via the object prototype. Prototype有许多很棒的功能,包括一个通过对象原型处理JS“继承”细节的Class帮助器

Edit: damn, I keep forgetting that jQuery (my own library of choice) has jQuery.extend 编辑:该死的,我一直忘记jQuery(我自己选择的库)有jQuery.extend

Doesn't he work for Yahoo? 他不为雅虎工作吗? You could always use the Yahoo User Interface libraries . 您始终可以使用Yahoo用户界面库

Personally, I'm partial to JQuery , as it strikes me as more concise, but you know: horses for courses. 就个人而言,我偏爱JQuery ,因为它让我更加简洁,但你知道:马匹是课程。

Dojo has followed Crockford's ideas very closely. Dojo非常密切地关注了Crockford的想法。 For example, there is an implementation of the code snippet you have above implemented under the function dojo.delegate (in an even faster form). 例如,有一个上面在函数dojo.delegate下实现的代码片段的实现(以更快的形式)。

I don't think there's a specific project that follow his recommendations to a tee. 我不认为有一个特定的项目遵循他对发球台的建议。 Most of the toolkits actually disagree with quite a few of his recommendations and patterns. 大多数工具包实际上不同意他的一些建议和模式。 If you're wondering about specific functionality, like that code snippet above, it would be worth asking about which specific tools from the book you want to use in your projects. 如果您想知道特定的功能,比如上面的代码片段,那么值得询问您希望在项目中使用哪些特定工具。

看看es5-shim ,一个为Object.create()和其他东西提供 - 尽管不完整 - 的垫片的库。

I think YUI is the closest to what Crockford teaches in his book. 我认为YUI最接近Crockford在他的书中所教导的内容。 (He's the Yahoo architect also and it makes sense) (他也是雅虎的架构师,这很有道理)

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

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