JavaScript学习笔记(一)—— 数据类型

一、基本数据类型:
 
1、js中基本数据类型:String、Boolean、Number、Null、undefined;
复合数据类型:object对象类型、Array数组类型
特殊对象:function函数类型

基本数据类型
 
① String  —字符串。
    若使用var声明变量,var x = “Andy” ,x 为字符串。将一个值转换成字符串:toString( )方法
 
② Boolean—布尔值。
    只有两个值:true或者false; 但true不一定等于1,false也不一定等于0;
    要将一个值转换为其对应的Boolean值,可以调用类型转换函数Boolean( )
 
③ Number —整数、浮点数、NaN.
    其中:任何数值除以0会返回NaN,而NaN除以任何数值也返回NaN;
    isNaN( )函数可以确定一个参数是否 “不是数值”。
    例如:alert(isNaN(“Andy”))// “Andy”不是数值,返回true;
            alert(isNaN(25))//25是数值,返回false;
            alert(isNaN(NaN ))//返回true;
 
ps:   Number()函数的转换规则:
       字符串:可用parseIn( )和parseFloat( )来转换成数值。
       Boolean值:true和false将分别替换为1和0
       null值:返回0
       undefined:返回NaN;
       对象:可调用valueOf( )方法,toString()方法
 
④ Null— 空。
    如果定义的变量在未来用于保存对象,最好将变量初始化为null;
    例:var a = null;
 
⑤ undefined—一个值:undefined。
    使用var声明变量但未对其加以初始化时,这个变量的值就是undefined。
    例:var a;
         alert(a);//undefined
 
PS:① null与undefined的区别:
         undefined没有必要显示设置值,即变量不含有值;
         null需要被初始化,用于、保存清空变量
     ②布尔值、数值、对象、字符串都有toString( )方法,null和undefined没有此方法。
 
复合数据类型
 
① object类型— 例:var a = new object( )
                     属性和方法:
                     constructor( )—保存着用于创建当前对象的函数
                     hasOwnProperty( )—检查该属性是否在当前对象实例中,是 返回 true;若在原型中则返回 false
                     isPropertyOf( )—检查传入的对象是否是另一个对象的原型
                     propertyIsEnumberable( )—检查给定的属性是否能够使用for-in语句来枚举
                     toString( )—对象转换为字符串表示
                     valueOf( )—对象转换为字符串、布尔值、数值表示
 
② Array类型—  例:var a = new Array();
                            a[0] = "123";
                            a[1] = "456";
                            alert(a.length); //打印出 2

特殊对象
 
function函数类型 —  可写成 var test = function(){}或 function test(){};
                             函数没有重载,总是指向最后定义的function;
                             内部特殊对象:arguments:  
                                                                   ① callee 对函数对象本身的引用,用于匿名函数的递归
                                                                   ② caller 只有在函数执行时才有定义,functionName.caller
                                                          this:   
                                                                   全局环境中this指向window
                                                                   事件处理函数中this指向绑定事件的节点
                                                                   构造函数中this指向被创建的对象
                                                                   call()  
                                                                   apply()   
 
判断JS中的数据类型
 
typeof、instanceof、constructor、prototype方法
 
① typeof:
             例 var a = 123;
                 var b = function(){
                   this.name = "Andy";
              }
                alert(typeof a);// number
                alert(typeof b); //function
                alert(typeof a == "number" );//true
                alert(typeof b == "boolean");//false
 
② instanceof
(判断已知对象类型的方法,instanceof 后面一定是对象类型,区分大小写):
                 例  alert(a instanceof Array);//false
                      alert(b instanceof Function );//true
 
③ constructor
(根据对象的constructor判断):
                例  alert(a.constructor === Array);//false
                     alert(b.condtructor === Function);//true
 
④ prototype :
                例     alert(Object.prototype.toString.call(a)==="[object Number]");//true
                        alert(Object.prototype.toString.call(b)==="[object String]");  //false
 
 
typeof方法:
          ①numberstringbooleanfunction, undefined可以被正确检测出;
                 ②null,array,json,date,reg,error 全部被检测为 object类型;
                 ③typeof 区分不出 json和array类型,输出都是 object;
 
instanceof 方法:
                 ①array、function、json、date、reg、error可以检测为true;
                 ②number、string、boolean、undefined、null检测为false;
                 ③null 和 undefined 检测不成object或其他类型
                 因此,要用instanceof 方法,首先判断 是否为null和undefined类型。
 
constructor 方法:
                          除了null和undefined ,其他类型均能使用constructor判断
                          但被判断的array必须在当前页面声明,例如对应子页面的Array 用父页面来判断,会返回false。
 
prototype 方法:
                       Object.prototype.toString.call(变量) 可以检测出所有类型;
                       例如 Object.prototype.toString.call(date); //返回[object Date]

jquery中$.type的实现

   检测出所有类型,和 Object.prototype.toString.call(date)方法很像,输出结果为prototype方法输出的第二个参数,即:

   $.type(num);  //number

   $.type(nul);  // null

原文地址:https://www.cnblogs.com/emory/p/5008482.html