javascript编写_编写JavaScript时最常犯的六个错误

javascript编写

While there are many possible errors made in JavaScript, these are the most common mistakes made in day-to-day use:

尽管JavaScript中可能发生许多错误,但这些是日常使用中最常见的错误:

编写嵌入式或嵌入式JavaScript,而不是链接 (Writing embedded or inline JavaScript, rather than linked)

We link JavaScript files for much the same reasons we write the majority of our CSS in an external style sheet: doing so separates behaviour from presentation, makes the code earlier to maintain, and allows the same script to be used on multiple pages without replication or redundancy. There are reasons for embedding your JavaScript code directly on a page, but doing so should be considered soberly.

我们链接JavaScript文件的原因与在外部样式表中编写大多数CSS的原因大致相同:这样做可以将行为与表示分离开来,使代码可以更早地维护,并允许同一脚本在多个页面上使用而无需复制或冗余。 将您JavaScript代码直接嵌入到页面中是有原因的,但应谨慎考虑。

不延迟脚本的执行 (Not delaying the execution of scripts)

If you place JavaScript in the <head> section of your HTML document, the script will potentially start before any <body> content appears. This problem is frequently encountered by neophyte developers, who write JavaScript code before DOM content, creating conditions in which the script has nothing to work on, which leads to errors.

如果将JavaScript放在HTML文档的<head>部分中,该脚本可能会在出现任何<body>内容之前启动。 新手开发人员经常遇到此问题,他们在DOM内容之前编写JavaScript代码,从而创建了脚本无法处理的条件,从而导致错误。

There are several possible ways to solve this problem. The first solution is the simplest: placing all your scripts at the very end of the document, just before the closing </body> tag. This guarantees that the script will execute after the body content has loaded… but you should read the last point in this list for an important exception.

有几种可能的方法可以解决此问题。 第一个解决方案是最简单的:将所有脚本放在文档的末尾 ,即</body>标记之前。 这样可以保证脚本将在body内容加载后执行……但是您应该阅读此列表的最后一点以了解重要的例外。

在JavaScript中更改大小写 (Changing case in JavaScript)

JavaScript is case sensitive. This is not only true of JavaScript code, but also of any variables or constants you create within it. tokenOne is a different variable from TokenOne. Scripts often break because you are trying to test or set the value of a variable that does not exist.

JavaScript区分大小写。 这不仅适用于JavaScript代码,还适用于您在其中创建的任何变量或常量。 tokenOne是与TokenOne不同的变量。 脚本经常中断,因为您尝试测试或设置不存在的变量的值。

令人困惑的串联与加法 (Confusing Concatenation & Addition)

Concatenation is the joining of two or more strings. Addition is what you learned in school. In JavaScript both operators use the same + symbol to achieve their ends. It’s important to remember what you are joining together, how and why, and anticipate the result, in order to avoid errors:

串联是两个或多个字符串的连接。 加法是你在学校学到的。 在JavaScript中,两个运算符都使用相同的+符号来达到目的。 重要的是要记住您要加入的内容,方式和原因,并预期结果,以避免错误:

In the console:

在控制台中:

console.log(10 + 12);
> 22

console.log(10+"2");
> “102”
// note that this is a string, not a numeral

token = 2;
console.log(10 + token)
> 12

token = "Two";
console.log(10 + token);
> “10Two”

单引号和双引号令人困惑 (Confusing single and double quotes)

Single or double quotes can be used interchangeably in most JavaScript operations. This works out well until you get them confused: using one to start a string and the other quote form to end it, for example.

单引号或双引号可以在大多数JavaScript操作中互换使用。 直到您对它们感到困惑之前,这一直很好:例如,使用一个字符串开始,使用另一个引号形式结束。

console.log('Two');
> Two

console.log("Two's");
> Two's

console.log('Two's');
// result is an error

将DOM与数据混淆 (Confusing the DOM with data)

Placing your scripts at the end of the page gives you access to the entire DOM of the page: the page structure, elements and the text within them. But this doesn’t necessarily mean that you have all the data: for example, JavaScript will know that you have an image on the page, but will not have the actual pixel data of the image, or details like it’s width and height, as the image is still loading in the microseconds after the JavaScript has executed. Getting around that problem involves actually checking that the images have loaded completely in JavaScript.

将脚本放在页面末尾可让您访问页面的整个DOM:页面结构,元素和其中的文本。 但这并不一定意味着您拥有所有数据 :例如,JavaScript会知道您在页面上有一个图像,但是将具有图像的实际像素数据或诸如宽度和高度之类的详细信息,例如执行JavaScript后,图片仍会在微秒内载入。 解决问题涉及实际检查图像是否已完全加载到JavaScript中。

翻译自: https://thenewcode.com/289/The-Six-Most-Common-Mistakes-Made-In-Writing-JavaScript

javascript编写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值