Javascript基础复习 数据类型

I like JavaScript. It is a language that is both powerful and flexible, but only if you know how to use it. Once you have mastered the JavaScript language you can build almost anything, and you can do so quickly and interactively.

If you think JavaScript is simple or primitive, then you have fallen into a trap. You will find that you have much company in this trap. The so-called JavaScript developers down there may tell you that some other language “X” is superior. They may even tell you that you would be better off with a system that transalates language X into JavaScript. To leave the trap and master JavaScript requires effort and dedication. I know, because back in 1997 I was there.

Since then I’ve learned the depth and breadth of JavaScript on my own, by studying the official specification. You can learn the complete language as well. If your title includes “JavaScript developer”, you should.

In this blog I am going to present short JavaScript program fragment and ask you to predict its output. If you are a JavaScript developer, you will find it to be child’s play. If you are still learning the language, you may have some trouble and I hope you’ll read the explanation that follows.

The following JavaScript code displays an alert box. What will the alert box contain?

1var five = 5;
2five.three = 3;
3alert(five + five.three);

Skip to the end of this article to learn the correct answer. Here’s an explanation of how JavaScript arrives at that answer.

There are just six types in JavaScriptObjectNumberStringBooleanNull, and Undefined.

Objects include arrays, functions, and ordinary objects. Numbers may be integers or floating point or the special values NaN andInfinity. Strings include the empty string, "". Booleans have only two values: true and false. The last two primitive types are a little unusual: the only value whose type is Null is null, and the only value whose type is Undefined is undefined. All of the types except Object are also called “primitive”. The type of a JavaScript variable is not declared explicitly, it is inferred by the JavaScript runtime. In this case the type of the variable called five is Number because it has been assigned a Number literal.

Just like many other computer languages, JavaScript will implicitly convert the type of a value to suit the operator that is being applied to the value. Unlike many computer languages, JavaScript is very aggressive about this. For example the result of "5" - "3" is the Number 2 because the minus operator converts both of its operands to Numbers. If an operand can’t be converted to a Number, then NaN (“Not a Number”) is used instead. For example "5" - "Fred" is implicitly converted to 5 - NaN, which yields NaN.

The complete set of rules for implicit type conversions isn’t terribly complicated so long as you know what type of operands each operator requires.

The Object and String rules that follow state that initially the Object “value is converted to a primitive”. If the operand’s type must be a Number, this means that the JavaScript engine calls the object’s valueOf() method and if the result is not primitive, then the result is converted to a String with the object’s toString() method. If the operand’s type must be a String, the process begins by calling the object’s toString() method, and if its result is not primitive then the valueOf() method is applied to that. In either case, if the result still isn’t a primitive, then an exception is thrown.

If the operand’s type must be a Number but the type of the operand‘s value is:

Object:
the value is converted to a primtive and if the result is not a Number then one of the following conversions is applied
String:
the String is converted to a Number per the usual JavaScript rules
Boolean:
1 if the value is true, otherwise 0
Null:
0
Undefined:
NaN

If the operand’s type must be a String but the type of the operand’s value is:

Object:
the value is converted to a primtive and if the result is not a String then one of the following conversions is applied.
Number:
the number as a String, e.g. "123" or "12.34"
Boolean:
"true" or "false"
Null:
"null"
Undefined:
"undefined"

If the operand’s type must be a Boolean but the type of the operand’s value is:

Object:
true
Number:
false if the value is zero, otherwise true
String:
false if the value is the empty string, otherwise true
Null:
false
Undefined:
false

If the operand’s type must be an Object but the type of the operand’s value is:

Number:
the value is boxed with a Number object with new Number(value)
String:
the value is boxed with a String object with new String(value)
Boolean:
the value is boxed with a Boolean object with new Boolean(value)
Null:
an exception will be thrown
Undefined:
an exception will be thrown

Now that the type conversion rules are clear, let’s return to the example.

1var five = 5;
2five.three = 3;
3alert(five + five.three);

As we noted before, the first line creates a variable called five whose type is Number.

When the property accessor is applied to the variable five, its type is converted to Object. This is called “boxing” and it relies on theNumber constructor, which produces an Object, not a Number primitive. The second line of the example is equivalent to this:

1(new Number(five)).three = 3;

As you can see, we have not saved a reference to the new Number Object in any variable. After this expression has been evaluated, the Number Object whose three property was set is discarded. The five variable remains unchanged.

The third line’s five.three expression causes another Number Object to be created. Since the new Object has no threeproperty, the special undefined value is returned. The result is equivalent to this:

1alert(5 + undefined);

The addition operator converts both of its operands to Numbers. In this case undefined is converted to NaN which yields:

1alert(5 + NaN);

And that is why the Alert box in our example just displays NaN.  


原文:http://blogs.adobe.com/webplatform/2012/08/27/javascript-types/ 

原文地址:https://www.cnblogs.com/taoqianbao/p/2721038.html