博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Javascript基础复习 数据类型
阅读量:6092 次
发布时间:2019-06-20

本文共 5721 字,大约阅读时间需要 19 分钟。

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 . 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?

1 var five = 5;
2 five.three = 3;
3 alert(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 : , , , , , and .

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 . 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 (“”) 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 “”. 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  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  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  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  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.

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

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

When the  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:

1 alert(5 + undefined);

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

1 alert(5 + NaN);

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

原文: 

 

 

转载于:https://www.cnblogs.com/taoqianbao/archive/2012/10/12/2721038.html

你可能感兴趣的文章
暴力屏蔽80访问失败的用户
查看>>
营销型后台系统开发应该考虑到的
查看>>
vue-admin-template 切换回中文
查看>>
java模式之模板模式——抽象类
查看>>
[ACM] hdu 1251 统计难题 (字典树)
查看>>
调试json
查看>>
C - Surprising Strings
查看>>
hibernate里的generator中class =value介绍
查看>>
activity-alias的使用
查看>>
第36周日
查看>>
SQL Server 无法打开物理文件的 2 种解决办法
查看>>
推荐一款好用的文件/文件夹对比工具 —— Beyond Compare
查看>>
java设计模式--结构型模式--桥接模式
查看>>
JS window.open()属性
查看>>
手机管理中的应用【6】——电源管理篇
查看>>
【Android工具】DES终结者加密时报——AES加密演算法
查看>>
效果收集-点击显示大图
查看>>
Android 开机过程PMS分析
查看>>
找不到com.apple.Boot.plist
查看>>
使用openssl创建自签名证书及部署到IIS教程
查看>>