Home  >  Article  >  Web Front-end  >  Some designs that Javascript needs to pay attention to (organized a small part)

Some designs that Javascript needs to pay attention to (organized a small part)

php是最好的语言
php是最好的语言Original
2018-08-01 09:26:251243browse

I have been reviewing avascript these days. Since this great js was developed by a very powerful person in ten days, there will always be some design flaws. Let us summarize in the study and skip these pitfalls. .

1. Operators == and ===

Pay special attention to the equality operator ==. When JavaScript is designed, there are two comparison operators:

The first one is == comparison, which will automatically convert the data type and then compare. In many cases, very strange results will be obtained. ;

The second type is === comparison, which will not automatically convert the data type. If the data types are inconsistent, false will be returned. If they are consistent, compare again.

Due to this design flaw in JavaScript, Don't use == comparisons and always stick to === comparisons.

2. NaN

NaN表示Not a Number,当无法计算结果时用NaN表示

NaNThis special Number is not equal to all other values, including itself

NaN === NaN; // false

The only way to judge## The method of #NaN is through the isNaN() function:

isNaN(NaN); // true

3. null and undefined

null represents an "empty" The value is different from 0 and the empty string ''. 0 is a numerical value, and '' represents a length of 0 String, while null means "empty".

In other languages, there are also expressions similar to JavaScript's

null. For example, Java also uses null, Swift uses nil, and Python uses #. ##None means. However, in JavaScript, there is also an undefined similar to null, which means "undefined". The designers of JavaScript intended to use

null

to represent an empty value, and undefined to represent an undefined value. Facts have proved that this is of no use, and the difference between the two is of little significance. In most cases, we should use null. undefinedIt is only useful when judging whether the function parameters are passed 4. Array

An array is a set arranged in order, and each value of the set is called element. JavaScript arrays can contain any data type. For example:

[1, 2, 3.14, 'Hello', null, true];

Initialize the array:

var array1 = new Array()

var array1 = []

var array1 = new Array(10)

5. Object

The object of JavaScript is an unordered set composed of keys and values

var person = {
    name: 'Bob',
    age: 20,
    tags: ['js', 'web', 'mobile'],
    city: 'Beijing',
    hasCar: true,
    zipcode: null
};

The keys of JavaScript objects are all string types, and the values ​​can be Any data type. The above

person

object defines a total of 6 key-value pairs, each of which is also called an attribute of the object. For example, the name attribute of person is 'Bob', zipcode attribute is null. To get the properties of an object, we use the

object variable. Property name

method: <pre class="brush:js;toolbar:false;">person.name; // &amp;#39;Bob&amp;#39; person.zipcode; // null</pre> Related articles:

Required during the use of JavaScript Things to pay attention to and some basic grammar_Basic knowledge

Issues that need to be paid attention to in web front-end design

Related videos:

JavaScript design patterns video tutorial

The above is the detailed content of Some designs that Javascript needs to pay attention to (organized a small part). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn