JavaScript 变量类型

值类型(基本类型):

字符串(String)、数字(Number)、布尔(Boolean)、对空(Null)、未定义(Undefined)、Symbol。

引用数据类型:

对象(Object)、数组(Array)、函数(Function)。

注:Symbol 是 ES6 引入了一种新的原始数据类型,表示独一无二的值。

JavaScript 拥有动态类型

JavaScript 拥有动态类型。这意味着相同的变量可用作不同的类型:


var x;               // x 为 undefined

var x = 5;           // 现在 x 为数字

var x = "John";      // 现在 x 为字符串

JavaScript 字符串

字符串是存储字符(比如 "Bill Gates")的变量。

字符串可以是引号中的任意文本。您可以使用单引号或双引号:


var carname="Volvo XC60";

var carname='Volvo XC60';

您可以在字符串中使用引号,只要不匹配包围字符串的引号即可:


var answer="It's alright";

var answer="He is called 'Johnny'";

var answer='He is called "Johnny"';

JavaScript 数字

JavaScript 只有一种数字类型。数字可以带小数点,也可以不带:


var x1=34.00;      //使用小数点来写

var x2=34;         //不使用小数点来写

极大或极小的数字可以通过科学(指数)计数法来书写:


var y=123e5;      // 12300000

var z=123e-5;     // 0.00123

JavaScript 布尔

布尔(逻辑)只能有两个值:true 或 false。


var x=true;

var y=false;

JavaScript 数组

下面的代码创建名为 cars 的数组:


var cars=new Array();

cars[0]="Saab";

cars[1]="Volvo";

cars[2]="BMW";


var cars=["Saab","Volvo","BMW"];

JavaScript 对象

对象由花括号分隔。在括号内部,对象的属性以名称和值对的形式 (name : value) 来定义。属性由逗号分隔:


var person={firstname:"John", lastname:"Doe", id:5566};

上面例子中的对象 (person) 有三个属性:firstname、lastname 以及 id。

空格和折行无关紧要。声明可横跨多行:


var person={

firstname : "John",

lastname  : "Doe",

id        :  5566

};

对象属性有两种寻址方式:


name=person.lastname;

name=person["lastname"];

Undefined 和 Null

Undefined 这个值表示变量不含有值。

可以通过将变量的值设置为 null 来清空变量。


cars = null;

person = null;

2 检查变量类型

使用typeof检测

我们平时用的最多的就是用typeof检测变量类型了


console.log(

    typeof num,

    typeof str,

    typeof bool,

    typeof arr,

    typeof json,

    typeof func,

    typeof und,

    typeof nul,

    typeof date,

    typeof reg,

    typeof error

);

// number string boolean object object function undefined object object object object

从输出的结果来看,arr, json, nul, date, reg, error 全部被检测为object类型,其他的变量能够被正确检测出来。当需要变量是否是number, string, boolean, function, undefined, json类型时,可以使用typeof进行判断。其他变量是判断不出类型的,包括null。

还有,typeof是区分不出array和json类型的。因为使用typeof这个变量时,array和json类型输出的都是object。

使用instance检测

在 JavaScript 中,判断一个变量的类型尝尝会用 typeof 运算符,在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 "object"。ECMAScript 引入了另一个 Java 运算符 instanceof 来解决这个问题。instanceof 运算符与 typeof 运算符相似,用于识别正在处理的对象的类型。与 typeof 方法不同的是,instanceof 方法要求开发者明确地确认对象为某特定类型。例如:


obj instanceof Array    // true or false

obj instanceof Object   // true or false

使用constructor检测

在使用instanceof检测变量类型时,我们是检测不到number, 'string', bool的类型的。因此,我们需要换一种方式来解决这个问题。

constructor本来是原型对象上的属性,指向构造函数。但是根据实例对象寻找属性的顺序,若实例对象上没有实例属性或方法时,就去原型链上寻找,因此,实例对象也是能使用constructor属性的。


function Person(){



}varTom =new Person();

  // undefined和null没有constructor属性console.log(

    Tom.constructor==Person,

    num.constructor==Number,

    str.constructor==String,

    bool.constructor==Boolean,

    arr.constructor==Array,

    json.constructor==Object,

    func.constructor==Function,

    date.constructor==Date,

    reg.constructor==RegExp,

    error.constructor==Error

);// 所有结果均为true

从输出的结果我们可以看出,除了undefined和null,其他类型的变量均能使用constructor判断出类型。

不过使用constructor也不是保险的,因为constructor属性是可以被修改的,会导致检测出的结果不正确,例如:


function Person(){



}function Student(){



}

Student.prototype =new Person(); varJohn =new Student();

console.log(John.constructor==Student);// false console.log(John.constructor==Person);// true

在上面的例子中,Student原型中的constructor被修改为指向到Person,导致检测不出实例对象John真实的构造函数。

同时,使用instaceof和construcor, 被判断的array必须是在当前页面声明的!比如,一个页面(父页面)有一个框架,框架中引用了一个页面(子页面),在子页面中声明了一个array,并将其赋值给父页面的一个变量,这时判断该变量,Array == object.constructor; 会返回false;

原因:

1、array属于引用型数据,在传递过程中,仅仅是引用地址的传递。

2、每个页面的Array原生对象所引用的地址是不一样的,在子页面声明的array,所对应的构造函数,是子页面的Array对象;父页面来进行判断,使用的Array并不等于子页面的Array;切记,不然很难跟踪问题!

使用Object.prototype.toString.call

console.log(

    Object.prototype.toString.call(num),

    Object.prototype.toString.call(str),

    Object.prototype.toString.call(bool),

    Object.prototype.toString.call(arr),

    Object.prototype.toString.call(json),

    Object.prototype.toString.call(func),

    Object.prototype.toString.call(und),

    Object.prototype.toString.call(nul),

    Object.prototype.toString.call(date),

    Object.prototype.toString.call(reg),

    Object.prototype.toString.call(error)

);// '[object Number]' '[object String]' '[object Boolean]' '[object Array]' '[object Object]'// '[object Function]' '[object Undefined]' '[object Null]' '[object Date]' '[object RegExp]' '[object Error]'

从输出的结果来看,Object.prototype.toString.call(变量)输出的是一个字符串,字符串里有一个数组,第一个参数是Object,第二个参数就是这个变量的类型,而且,所有变量的类型都检测出来了,我们只需要取出第二个参数即可。或者可以使用Object.prototype.toString.call(arr)=="object Array"来检测变量arr是不是数组。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容