I like JavaScript. it is a powerful and flexible language, provided that you know how to use it correctly. once you have mastered JavaScript, you can use it to do everything quickly and well. if you think JavaScript is too simple or too low-level... syntaxHighlighter. all
I like JavaScript. it is a powerful and flexible language, provided that you know how to use it correctly. once you have mastered JavaScript, you can use it to do everything quickly and well.
If you think JavaScript is too simple or too low-level, you have fallen into a trap. and you will find that many people have fallen into such traps. these so-called JavaScript developers may tell you that some other languages "X" are better.
They might even say that it would be nice if there was a system that could convert the X language to JavaScript. it takes a lot of effort and contribution to escape this trap until you truly grasp JavaScript. believe me, because I have been learning JavaScript since 1997.
I learned all the advanced knowledge about JavaScript by studying the official standard documentation, so you can also master the complete language knowledge in this way. if your title includes "JavaScript developers", you should do so.
In this blog, I will give you a short JavaScript code snippet and let you provide the correct output of this Code. if you are a JavaScript developer, you will find that such a question is too simple. if you are still learning this language, you may encounter some difficulties, but you can read the explanation below.
The following JavaScript code shows a pop-up box. What is displayed in the pop-up box?
Var five = 5;
Five. three = 3;
Alert (five + five. three );
Jump to the end of this article to view the correct answer. Next, why is there an explanation of this result.
In JavaScript, there are six data types: Object, Number, String, Boolean, Null, and Undefined.
The Objects type contains arrays, functions, and other general Objects. the numeric type can be integer or floating point, and the special values NaN and Infinity. the string (Strings) type contains an empty string ,"". the Booleans type has only two values: true and false. the last two basic data types are a bit special: the Null type has only one value: null, And the Undefined type has only one value: undefined. all types except objects are called "primitive )". the JavaScript variable type is not explicitly specified during definition, but is automatically inferred when the script is running. in the above Code, the variable named five is of the Number type, because it is assigned a Number literal 5.
Similar to other computer programming languages, JavaScript implicitly converts the type of a value to the type of an operator suitable for operating the value. different from other languages, JavaScript is very active in performing these conversions. for example, the calculation result of the expression "5"-"3" is number 2, because the minus sign operator converts all the operands on both sides to numbers before performing the operation. if an operand cannot be converted to a Number, NaN ("Not a Number") is used instead. for example, if the expression "5"-"Fred" is implicitly converted to 5-NaN, the operation result will also be NaN.
A complete set of implicit conversion rules is not very complex. You only need to know the type of operands required by each operator.
Conversion of any value to the original value follows the rule "conversion of any value to the original value ". the conversion from an object to the original value is complex: If the operand type must be Number, the JavaScript engine calls the valueOf () method of the object, if the returned result is still not an original value, the toString () method of the object will be called to convert it into a String type value. if the operand type must be String, the toString () method of the object is called first. If the returned result is not an original value, the valueOf () method of the object is called. in any case, if the final conversion result is still not an original value, an exception is thrown.
If the operand type must be a number, but the actual type of the operand is:
Object:
The value is first converted to the original starting value. If the conversion result is not a number, it will enter the following conversion branch.
String:
String-type values are converted to numeric values according to common JavaScript rules.
Boolean:
If the value is true, it is converted to 1. Otherwise, it is converted to 0.
Null:
0
Undefined:
NaN
If the type of the operand must be a string, but the actual type of the operand is:
Object:
The value is first converted to the original starting value. If the conversion result is not a string, it will enter the following conversion branch.
Number:
Convert a number to a string, for example, "123" or "12.34 ″
Boolean:
Convert to "true" or "false"
Null:
"Null"
Undefined:
"Undefined"
If the type of the operand must be a Boolean value, but the actual type of the operand is:
Object:
True
Number:
If the value is 0, convert it to false. Otherwise, convert it to true)
String:
If the value is an empty string "", convert it to false; otherwise, convert it to true.
Null:
False
Undefined:
False
If the type of the operand must be an object value, but the actual type of the operand is:
Number:
Use the Number of the packaging object type to wrap the original value, new Number (value)
String:
Use the wrap object type String to wrap the original value, new String (value)
Boolean:
Use the Boolean packaging object type to wrap the original value, new Boolean (value)
Null:
Throw an exception
Undefined:
Throw an exception
Now all the conversion rules are clear. Let's go back to the original example.
Var five = 5;
Five. three = 3;
Alert (five + five. three );
As we mentioned earlier, the first line of code creates a variable named five with the type of Number.
When attribute accessors are used on the variable five, their values are converted to the Object type. this operation is called "packaging" and depends on the Number constructor. This constructor will generate an object instead of an original value. the second line of code is actually equivalent to the following code:
(New Number (five). three = 3;
As you can see, we have not saved the reference of the new Number object to a variable. after the expression is run, the object added with the three attribute will be discarded. the value of the five variable has not changed.
The expression five. three in the third line of code will create a Number object again. This new object does not have the three attribute, so the returned Special Value undefined. The result is equivalent:
Alert (5 + undefined );
The addition operator converts all the operands on both sides to numbers. In this example, undefined is converted to NaN, which is:
Alert (5 + NaN );
This explains why only one NaN is displayed in the last pop-up box.
(Note: The last step is implicit conversion. When alert () is performed, NaN is automatically converted to "NaN ")
Comment:
Jerone:
Your Content in this article is only part of JavaScript. Specifically, it only contains ECMAScript. in addition, JavaScript also contains DOM, that is, more data types such as Node, Element, and HTMLElement.
Author's reply:
Node, Element, and HTMLElement are not data types. Like Array, Date, and RegExp, they all belong to the Object type.
Marcus Pope:
I think the correct writing method should be pointed out so that the following code can run normally.
Var five = new Number (5 );
Alert (five); // 5
Five. three = 3;
Alert (five + five. three); // 8
I also think it is best to use "number" to refer to the original value of a Number, and use "number" to refer to the packaging object of a Number type, so as to reduce the reader's confusion. this is also a string displayed when the typeof operator is used in JavaScript to view the internal class attributes of these values. (Note: typeof 5 = "number ")
ACRESTAN:
Yes, this article is confusing because the author uses a string of the type starting with an uppercase letter to describe the original value...
For example, "the first line creates a variable called five whose type is Number" is incorrect! Because your code proves this.
Metadings:
There is also the seventh data type, the most important type in JavaScript: Function ). in JavaScript, functions can be used to create new objects. Therefore, the original value type and function type are different.
Var Project = function (){};
// Project instanceof Function === true
// Project instanceof Object === true
// (Typeof Project === 'function') === true
Var o = new Project ();
// O instanceof Project === true
// O instanceof Object === true
// (Typeof Project === 'object') === true
Please read this link carefully at http://metadea.de/v/and check that I am a real-life criptcategory .)
Author's reply:
Obviously, a function is very important in JavaScript. It is worth explaining in a separate article, but the function is not another data type. A function is just a special object. although special, it is still an object.
Objects Created by many different constructors inherit the same type: Object. this is easy to detect: If Object (a) = a, a is an Object type, rather than other original value types.
Ulu:
This article further proves that JavaScript is not only the most powerful language, but also the most confusing language. avoid using it at any cost. one day, there will be a more user-friendly language integrated into all mainstream browsers, and I hope to catch up in my lifetime.
Author's reply:
When you hold a hammer in your hand, everything looks like a nail. When you think JavaScript is confusing, any feature can be your evidence.
Dave Chapman:
This type of Encoding Error is exactly why we need a better IDE that can check the syntax before running the Code while coding. for example. running the code in the example in closure compiler generates the following warning information:
"JSC_INEXISTENT_PROPERTY: Property three never defined on Number at line 4 character 13:
Alert (five + five. three );"
Closure compiler is Google's code zip or a compiler. https://developers.google.com/closure/compiler)
Simonleung:
A number can be of the "object" type or "number" type.
When used in conditional statements or other places that need to be converted to boolean values, these two types of numbers produce different results, such.
Number (0) will get true, because Number (0) is an object, and 0 will obviously get false.
(Translator's note: he is wrong here. number is only a type conversion function, and the returned value is still the original value. Only using new Number (0) can Number be regarded as a constructor, and the returned object is)
In addition, the typeof operation is performed on null and "object" is returned, but it is not a real object. null is a false value and will be converted to false.