js基础⑥

Posted on 2017-08-20 18:19  dyzne  阅读(148)  评论(0编辑  收藏  举报

一、typeof 

检测变量的数据类型“

typeof "John"                // 返回 string 
typeof 3.14                  // 返回 number
typeof false                 // 返回 boolean
typeof [1,2,3,4]             // 返回 object
typeof {name:'John', age:34} // 返回 object

ps:数组是特殊的对象类型,所以返回object

 

二、null

表示什么都没有,是个只有一个值得特殊类型,空对象引用

typeof null               // 返回 object

用来清空对象:

var my_name = null;  // 值 为 null(空), 但类型为对象

 

三、undefined

没有设置值的变量。

undefined也能清空对象:

var my_name = undefined;     // 值为 undefined, 类型为 undefined
清空变量:

 person = undefined;

typeof没有值得变量结果为undefined:

var person;                  // 值为 undefined(空), 类型是undefined
typeof person             //undefined

 

四、undefined与null区别

null 和 underfined 的值相等,但类型不等:

typeof undefined             // undefined
typeof null                  // object
null === undefined           // false
null == undefined            // true

 

五、数据类型

在 JavaScript 中有 5 种不同的数据类型:

  • string
  • number
  • boolean
  • object
  • function

3 种对象类型:

  • Object
  • Date
  • Array

2 个不包含任何值的数据类型:

  • null
  • undefined
typeof "John"                 // 返回 string 
typeof 3.14                   // 返回 number
typeof NaN                    // 返回 number
typeof false                  // 返回 boolean
typeof [1,2,3,4]              // 返回 object
typeof {name:'John', age:34}  // 返回 object
typeof new Date()             // 返回 object
typeof function () {}         // 返回 function
typeof myCar                  // 返回 undefined (如果 myCar 没有声明)
typeof null                   // 返回 object

 

ps:

  • NaN 的数据类型是 number
  • 数组(Array)的数据类型是 object
  • 日期(Date)的数据类型为 object
  • null 的数据类型是 object
  • 未定义变量的数据类型为 undefined

如果对象是 JavaScript Array 或 JavaScript Date ,我们就无法通过 typeof 来判断他们的类型,因为都是 返回 Object。通过下面的constructor属性。

 

六、constructor 

constructor 属性返回所有 JavaScript 变量的构造函数。

"John".constructor                 // 返回函数 String()  { [native code] }
(3.14).constructor                 // 返回函数 Number()  { [native code] }
false.constructor                  // 返回函数 Boolean() { [native code] }
[1,2,3,4].constructor              // 返回函数 Array()   { [native code] }
{name:'John', age:34}.constructor  // 返回函数 Object()  { [native code] }
new Date().constructor             // 返回函数 Date()    { [native code] }
function () {}.constructor         // 返回函数 Function(){ [native code] }

 

可以通过属性的值来判断具体是数组还是时间:

function isArray(myArray) {
    return myArray.constructor.toString().indexOf("Array") > -1;
} //判断是否为Array

function isArray(myArray) {
    return myArray.constructor.toString().indexOf("Date") > -1;
} //判断是否为Date

 

七、数据类型转换

 

1、数字转字符串

全局方法-- String()方法: 

String(x)         // 将变量 x 转换为字符串并返回
String(123)       // 将数字 123 转换为字符串并返回
String(100 + 23)  // 将数字表达式转换为字符串并返回

 

 Number方法--toString方法:

x.toString()
(123).toString()
(100 + 23).toString()

 

2、布尔值转字符串

全局方法-- String()方法:

String(false)        // 返回 "false"
String(true)         // 返回 "true"

 

Boolean方法--toString方法:

false.toString()     // 返回 "false"
true.toString()      // 返回 "true"

 

continue...