JavaScript存在哪些反模式? [英] What anti-patterns exist for JavaScript?

查看:117
本文介绍了JavaScript存在哪些反模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现不做的事情比学习应该做的更难学习。

I find that what not to do is a harder lesson to learn than what should be done.

根据我的经验,专家与中级人员之间的区别在于能够从各种看似相同的方式中做出相同的选择。

From my experience, what separates an expert from an intermediate is the ability to select from among various seemingly equivalent ways of doing the same thing.

那么,谈到JavaScript 你应该做什么样的事情以及为什么

So, when it comes to JavaScript what kinds of things should you not do and why?

我'能够为Java找到很多这些,但由于JavaScript的典型上下文(在浏览器中)与Java非常不同,我很想看看会发生什么。

I'm able to find lots of these for Java, but since JavaScript's typical context (in a browser) is very different from Java's I'm curious to see what comes out.

推荐答案

语言:


  • 通过在全局上下文中创建大量变量来进行命名空间污染。

  • Namespace polluting by creating a large footprint of variables in the global context.

以'foo.onclick = myFunc'形式绑定事件处理程序(不可扩展,应该使用attachEvent / addEventListener)。

Binding event handlers in the form 'foo.onclick = myFunc' (inextensible, should be using attachEvent/addEventListener).

在几乎所有非JSON上下文中使用eval

Using eval in almost any non-JSON context

几乎每次使用document.write(使用DOM方法)像document.createElement)

Almost every use of document.write (use the DOM methods like document.createElement)

Prot对Object对象进行类型化(BOOM!)

Prototyping against the Object object (BOOM!)

这是一个小的,但用'+'做大量的字符串连接(创建一个数组并加入它)效率更高)

A small one this, but doing large numbers of string concats with '+' (creating an array and joining it is much more efficient)

参考不存在的未定义常量

设计/部署:


  • (通常)不提供noscript支持。

  • (Generally) not providing noscript support.

不将代码打包到单个资源中

Not packaging your code into a single resource

内联(即

特定于Ajax:


  • 未向用户显示请求的开始,结束或错误

  • not indicating the start, end, or error of a request to the user

polling

传递和解析XML而不是JSON或HTML(如果适用)

passing and parsing XML instead of JSON or HTML (where appropriate)

编辑:我一直在考虑更多!

这篇关于JavaScript存在哪些反模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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