zoukankan      html  css  js  c++  java
  • JavaScript之数据类型

    数据类型

      JavaScript中有5种简单数据类型(也称为基本数据类型):Undefined、Null、Boolean、Number和String。还有1种复杂数据类型——Object,Object本质上是由一组无序的名值对组成的。

     typeof操作符

      介于JavaScript是松散类型的,因此需要有一种手段来检测给定变量的数据类型——typeof就是负责提供者方面信息的操作符。对一个值使用typeof操作符可能返回下列某个字符串:

      ● "undefined"——如果这个值未定义;

      ● "boolean"——如果这个值是布尔值;

      ● "string"——如果这个值是字符串;

      ● "number"——如果这个值是数值;

      ● "object"——如果这个值是对象或null;

      ● "function"——如果这个值是函数;

    ---------------------------------------------------------------------

    1)基本类型
    ---数字,采用IEEE754标准定义的64位浮点格式.
    特殊数值常量:
    Infinity                        无穷大的特殊值
    NaN                                非数字值
    Number.MAX_VALUE                可表示的最大数字
    Number.MIN_VALUE                可表示的最小数字
    Number.NaN                        非数字值
    Number.POSITIVE_INFINITY        正无穷大
    Number.NEGATIVE_INFINITY        负无穷大
    把数字转为字符串6种方式
    var n = 1.23456;
    var n_as_str = n+"";
    String(n);
    n.toString(x);            //x=2,binary; x=8, octonay; x=16,hexadecimal.if empty,decimal
    n.toFixed(x);             //小数点后位数
    n.toExponential(x);        //显示指数形式,x表示小数位
    n.toPrecision(x);        //若n位数>x时显示为指数,x表示数字的精度
    ---字符串
    字符串转为数字
    在数字环境,自动转换为数字,
    var num = "2" * "3"; //num = 6
    var num = str_val - 0;
    var num = Number(str_val); //以10为基数的数字有效,允许开头和结尾的空白
    parseInt(str)
    parseInt(str,radix) //the same with java
    parseFloat(str)
    ---布尔
    显式转换的方法
    var x_as_boolean = Boolean(x);
    var x_as_boolean = !!x;
    ---null
    表示"无值".
    对象转换:布尔环境式时,非空对象为false;字符串环境时"null";数字环境时0;
    ---undefined
    使用未声明的变量时,或使用声明但没有赋值的变量时,或使用不存在的对象属性时,返回
    undefined.
    对象转换:布尔环境式时,非空对象为false;字符串环境时"undefined";数字环境时NaN;
    与null区别:
    null是关键字,undefined不是.(ECMAScript v3定义了undefined的全局变量,初始值是undefined)

    <html>
      <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    	<title>null and undefined</title>
      </head>
    
      <body>
    	<p>比较null与undefined</p>
    
    	<script>
    	  var undef; 
    	  document.write("布尔环境: ")
    	  document.write(undef==null);      //true
    	  document.write("<br/>");
    	  document.write("字苻串环境: ")
    	  document.write("".undef);         //undefined
    	  document.write("<br/>");
    	  document.write("数字环境: ")
    	  document.write(1+undef);          //NaN
    	  document.write("<br/>");
    
    	  document.write("undef===null: ")
    	  document.writeln(undef===null);     //false
    	  document.write("<br/>");
    	  document.write("typeof undef: ")
    	  document.writeln(typeof undef);     //undefined
    	</script>
    
      </body>
    </html>

    2)复合类型
    对象:已命名的数据的集合
    对象直接量:由一个列表构成.列表的表式形式,{key:value,*};(key=标识符/字符串,value=常量/表达式)
    对象转换:布尔环境式时,非空对象为true;字符串环境时,toString();数字环境时,valueOf();
    数组
    不直持多维数组,数组元素可以是数组;
    数组元素不必据有相同的类型

    3)特殊对象
    函数
    一般语法,function func_name(args) {func_body;}
    lambda函数,function(args){func_body;}
    构造函数,new Function("args","func_body");

    *说明

    计划以后在note目录下发布些整理的笔记,好记心不如烂笔头.主要为了方便自己查找,若读者看了觉得哪儿理解不对,请指教.

    这篇是关于javascript的数据类型,主要内容来自"javascript权威指南"

  • 相关阅读:
    错误 2 error C2059: 语法错误:“::”
    完全卸载session 所需要的函数
    header("Location:http://www.baidu.com");
    php str_pad() 用法
    php str_pad();
    设计模式系列-01-开篇
    博客园样式的设置系列-01-侧边栏和皮肤的设置
    vs20132015UML系列之-类图
    php获取当前时间和转换格式
    saltstack:multi-master configuration
  • 原文地址:https://www.cnblogs.com/aure/p/4464325.html
Copyright © 2011-2022 走看看