Javascript学习总结--数据类型

来源:互联网 发布:照片转十字绣软件 编辑:程序博客网 时间:2024/05/19 03:25

一.数据类型

基本数据类型 :
Undefined Null String Number Boolean

复杂数据类型:
Object

二、各数据类型知识点

2.1 Undefined 与Null

(1)

Undefined 只有一个值:undefined
Null 只有一个值:null

(2)undefined 与null区别


  • undifined:表示”缺少值”,就是此处应该有一个值,但是还没有定义

  1. 使用var声明的变量但是未初始化,变量的值就是undefined
  2. 调用函数时,应该提供的参数没有提供,该参数等于undefined
  3. 对象没有赋值的属性,其值为undefined
  4. 函数没有返回值时,默认返回undefined
  5. 转换为数值是0
  • null:表示”没有对象”,即该处不应该有值; null值表示一个空对象指针

    1. 定义的变量将来用于保存对象,最好将变量初始化为null而不是其他值
    2. 对象原型链的终点: Object.getPrototypeOf(Object.prototype) //null
    3. 转换为数值是 NaN
  • null == undefined
    结果为true
  • 2.2 Number

    IEEE754表示整数和浮点数值
    整数:十进制,八进制(首位0),十六进制(前两位0x)
    浮点数:

    1. 数值必须包含一个小数点,小数点至少有一位数字;
    2. 如果浮点数值本身表示的就是整数(如1.0),则该值会转换为整数;
    3. ECMAScript 会将小数点后带6个零以上的浮点数值转换为以e表示法表示的数值
    4. 浮点数的最高精度是17位小数
    5. 基于IEEE754浮点数值的计算会产生舍入误差的问题,例如:
      不要使用:a+b == 0.3

    数值范围:

    1. Number.POSITIVE_INFINITY: Infinity 正无穷 ;
      Number.NEGATIVE_INFINITY: -Infinity 负无穷
    2. 确定一个数值是不是有穷的:
      isFinite(variable)
      参数位于最大值与最小值之间会返回true

    NaN:
    not a number

    1. 在ECMAScrtipt中,任何数值除以0都返回NaN
    2. 任何涉及NaN的操作,都返回NaN
    3. NaN与任何值都不相等,包括NaN本身: NaN == NaN // false
    4. isNaN(variable)

      • 接受任何类型的参数,该函数确定该参数是否“不是数值”
      • isNaN会尝试将传入的参数值转换为数值,任何不能被转换为数值的值返回true
        isNaN(NaN) // true
        isNaN(10) // false(10是一个数值)
        isNaN("10") // false(“10”可以被转换为数值10)
        isNaN("blue") // true(不能转换为数值)
        isNaN(true) // false(可以转换为数值1)
      • 也适用于对象。在基于对象调用时,会先调用对象的valueOf() 函数,然后确定该方法返回的值是否可以转换为数值;如果不能,则调用对象的 toString() 函数,再测试返回值。

    类型转换:Number() parseInt() parseFloat()

    Number()转换规则


    1. Boolean值,true 转换为 1,false 转换为 0
    2. 数字值,只是简单的传入传出
    3. null 转换为 0;undefined 转换为 NaN
    4. 字符串:

    4.1 字符串中只包含数字(包括正负号):转换为十进制数值,注意前导0会被忽略
    4.2 有效的浮点数会转化为浮点数值
    4.3 有效的十六进制转换为相同大小的十进制数值
    4.4 空字符串转换为 0
    4.5 如果字符串包含上述格式之外的字符,转换为 NaN
    4.6 不能正确转换八进制(开始的0被识别为前导0而忽略)

    parseInt()转换规则

    1. 对字符串进行转换时,会忽略字符串前面的空格,直到找到第一个非空格字符串。
    2. 如果第一个字符不是数字字符(可以识别各种进制的整数格式)或负号,则返回NaN;
    3. 如果第一个字符是数字字符,则会继续解析第二个字符,直到完全解析或者遇到非数字字符
    4. 如字符串是以“0x”开头,且后面跟数字字符,则作为十六进制整数,返回转换为十进制的数值;
    5. 如果是以“0”开头且后面跟数字字符,则作为八进制 (ECMAScript 3),在ECMAScript 5中,不能解析八进制,会忽略前导0
      可以提供第二个参数–转换进制时使用的基数
    var num1 = parseInt("123456zzz"); // 123456var num2 = parseInt(""); // NaNvar num3 = parseInt("-1"); // -1var num4 = parseInt("+5"); // 5var num5 = parseInt("07"); // 7var num6 = parseInt("08"); // 8var num7 = parseInt("0xA"); // 10var num8 = parseInt("0xAZ"); // 10var num9 = parseInt("22.5"); // 22

    2.3 String

    1. 单引号或双引号表示
    2. 字符串一旦创建,其值不可变
    3. 字符字面量:
      • \n 换行
      • \t 制表
      • \b 退格
      • \r 回车
      • \f 进纸
      • \\ 斜杠
      • \'单引号
      • \" 双引号
      • \xnn 以十六进制nn表示的一个字符(其中n为0~F)
      • \unnn 以十六进制nnnn表示的Unicode字符(其中n为0~F)
    4. 将值转换为字符串
      a. 使用加号操作符
      b. 使用toString()方法—注意:null 和undefined没有这个方法
      c. 使用String()不知道是否是null 或undefined可使用这个方法
      • null转换为“null”
      • undefined转换为 “undefined”

    2.4 Boolean

    只有两个字面值:true false

    数据类型 转换为true的值 转换为false的值 Boolean true false String 任何非空字符串 空字符串 Number 任何非0数字值(包括无穷大) 0 和 NaN Object 任何对象 null Undefined N/a undefined

    2.5 Object

    Object的每个实例都具有下列属性和方法:

    constructor
    hasOwnProperty(propertyname)
    isPrototypeOf(object)
    propertyIsEnumerable(propertyname)
    toLocaleString()
    toString()
    valueOf()

    参考:
    【1】http://www.ruanyifeng.com/blog/2014/03/undefined-vs-null.html
    【2】Javascript 高级程序设计 美 Nicholas

    三、数据类型检测方法

    3.1 使用typeof检测

    typeof 可以检测出的类型有:
    undefined 、 number 、string、 boolean 、function、 object

    var und;typeof und ; // "undefined"var und1 = undefined;typeof und ; // "undefined"
    var num = 123;typeof num; // "number"
    var str = "this is a string";typeof  str; // "string"
    var bool = true;typeof bool; // "boolean"
    var nul = null;typeof nul; // "object"var arr = [];typeof arr; // "object"var arr1 = new Array();typeof arr1; // "object"var arr2 = [1,2,3];typeof arr2; // "object"var obj = {};typeof obj; // "object"
    var date = new Date();typeof date; // "object"var reg = /^[a-zA-Z]{5,20}$/;typeof reg; // "object"var error= new Error();typeof error; // "object"
    var func = function () {    console.log("这是一个函数表达式");}typeof func; //"function"typeof alert; //"function"function func1() {    console.log("this is a function");}typeof func1; //"function"

    3.2 使用instanceof检测

    var  arr = [1,2,3];arr instanceof Array;// truearr instanceof Object;// truevar number1 = 2;number1 instanceof Number;// falsevar number2 = new Number(2);number2 instanceof Number;//true

    注意:
    (1)instanceof 会沿着原型链查找,只要在原型链上返回的都是true
    (2)注意基本类型的检测

    3.3 使用constructor检测

    var  arr = [1,2,3];arr.constructor;// function Array() { [native code] }arr.constructor == Array;// truearr.constructor == Object;// falsevar arr1= new Array();arr1.constructor == Array;// truevar obj = {"a":1,"b":2}obj.constructor;// function Object() { [native code] }

    constructor 属性返回对创建此对象的数组函数的引用

    3.4 使用Object.prototype.toString.call

    Object.prototype.toString.call(undefined);// "[object Undefined]"Object.prototype.toString.call(null);// "[object Null]"Object.prototype.toString.call(1);// "[object Number]"Object.prototype.toString.call(NaN);// "[object Number]"Object.prototype.toString.call("1");// "[object String]"Object.prototype.toString.call({});// "[object Object]"Object.prototype.toString.call([]);// "[object Array]"Object.prototype.toString.call(Math);//"[object Math]"Object.prototype.toString.call(JSON);//"[object JSON]"Object.prototype.toString.call(Date);// "[object Function]"Object.prototype.toString.call(new Date());// "[object Date]"Object.prototype.toString.call(new RegExp());//"[object RegExp]"Object.prototype.toString.call(function () {});// "[object Function]"Object.prototype.toString.call(window);// "[object global]"

    Object.prototype.toString.call(xxx)返回的是字符串

    3.5 检测一个变量是否是数组类型

    先检测浏览器是否支持Array.isArray(), 如果不支持则用Obejct.prototype.toString.call方法

    function isArray(value){    if (typeof Array.isArray === "function") {        return Array.isArray(value);    } else {        return Object.prototype.toString.call(value) === "[object Array]";    }}

    参考:
    《Javascript 高级程序设计(第三版)》
    http://www.cnblogs.com/wangying731/p/5165408.html
    http://www.cnblogs.com/flyjs/archive/2012/02/20/2360504.html

    原创粉丝点击