A list of Javascript pitfalls to help avoid mistakes_javascript tips
翻译讲究"信雅达",我就谈不上了.希望能把文章的意思不要弄错就行.
编程的陷阱(gotcha)是指计算机系统中的意想不到的文档特征而不是bug.这些陷阱使得初学者远离javascript编程.在我看来,因为所有的浏览器都能运行javascript使得它是使用最广泛的语言之一,但它也是最少人研究的.让我们从一个基础的示例开始.
1.浮点运算
这可能是挫败一些对javascript不熟悉并准备执行一些数学运算的人的主要原因.
- <script> </script>
- alert(0.02 / 0.1); //0.19999999999999998
- alert(1.14 * 100); //113.99999999999999 ;)
-
Math.round()就能在这里派上用场.
2.加号操作符的重载
"+"加号运算符即能做算术运算,又能够做字符串的连接.如果正确的使用它是很便利的.让我们看一看.
- <script> </script>
- var msg, one="1";
- msg = 2 + "1"; // msg = "21"
- msg = 2 + one; // msg = "21"
- msg = 1 + 1 + 1 + " musketeers"; // msg = "3 musketeers"
- msg = "Bond " + 0 + 0 + 7; //msg = "Bond 007"
-
上述行为是因为这些运算都是从左到右执行的.类型的转换是基于其中的字符串或数字.
3.行尾插入分号
javascript 自动在行尾插入分号";",让我们来看看这在一个简单的示例中的情况.
- <script> </script>
- function returnSame(a){
- return //Inserts semi-colon to convert to return;
- a //a becomes a; - Unreachable
- }
- alert(returnSame(2)); //Output is undefined
-
当在创建对象或使用对象的值的时候这个神奇的分号能使事情变得更复杂.
4.typeof操作符
typeof 是一个一元操作符,运算结果往往并不是如预期的那样.令人吃惊的是对"null"的运算结果是"object"
- <script> </script>
- var obj={}; //object created using object literal
- var arr=[]; //array created by array literal
- alert(typeof(obj)); //object - Good
- alert(typeof(arr)); //object - Bad
-
alert(typeof(null)); //object - Ugly! ;)
它仅仅能查找对象的原始类型.
5. false, null, undefined, NaN, Infinity
尽管他们看起来相似,但他们代表着不通的意思.javascript有3种基本数据类型数字numbers, 字符串strings 和布尔 boolean,除此之外还有两个不重要的数据类型"undefine"和"null".按照"=="运算符运算,null和undefine是相等的.
- <script> </script>
- var a;
- alert (a); //undefined
- alert (1/0); //Infinity
- alert (0/0); //NaN
- 0/0 == 0/0; //false - a NaN != NaN
- alert (b); //error
-
6.字符串只替换第一个匹配的字符
与PHP或其他程序语言不同,默认情况下,javascript的字符替换只替换第一个出现的匹配的字符.
- <script> </script>
- var nospace = "I dont need spaces".replace(" ","_");
- alert(nospace); //I_dont need spaces - Only first occurence
- var nospace = "I dont need spaces".replace(/ /g,"_");
- alert(nospace); //I_dont_need_spaces
-
7.parseInt function
parseInt is used to convert a string to an integer type. This function can pass in two parameters. The second parameter is the specified base. Here the decimal is specified with 10. If the base is not specified, the parseInt function It will try to find the appropriate base. If so, strings starting with 0 will be converted to octal.
- <script> </script>
- var str = "017";
- var strInt = parseInt(str); //strInt = 15 ;)
- var strInt = parseInt(str,10); //strInt = 17
-

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
