您的位置:首页 > Web前端 > JavaScript

重新学习JavaScript第一天

2016-10-15 18:54 323 查看
1.数据类型

原始数据:number,string,boolean,null,undefined

对象类型:object,包括function,array,date…

2.隐式转换

Number->string : 123+””;

String->number: “123”+0;

A==b:(弱等于)

“1.23”==1.23;

0==false;

Null==undefined;

New Object() == new Object();

[1,2] == [1,2];

A === b:(强等于,类型不同,直接返回false)

Null === null;

Undefined === undefined;

NaN != NaN;

Object != object

3.包装对象

基本数据类型在运行过程中会转换成包装对象。

“123”-> string object

123->number object

False->Boolean object

Var a =”string”;

Alert(a.length);//6

A.t = 3;// 虽然转成了包装类型可以赋值,但是赋值完就被销毁了,所以输出是”undefined”

Alert(a.t);//undefined;

4.类型检测

1.Typeof返回字符串,适合判断基本数据类型,遇到null失效

2.Instanceof 判断自定义对象类型,执行原理,去对比对象的prototype属性

function Person(){}

function Student(){};

Student.prototype = new Person();

Student.prototype.constructor = Student;

var aa = new Student();

aa instanceof Student;//true

aa instanceof Person;//true

var bb = new Person();

bb instanceof Person;//true

bb instanceof Student;false

3.Object.prototype.toString:适合判断内置对象和基元类型,遇到null和undefined失效Object.prototype.toString.apply([]) === “[object Array]”

Object.prototype.toString.apply(function(){}) === “[object Function]”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: