2019-10-27(类型,值和变量)

e.className获取e的class列表

事件

点击事件:onclick;‘load’事件:文档加载完成以后触发

window.onload=function(){}

词法结构

字符集:Unicode编码

js区分大小写,html不区分大小写

数据类型

原始类型:数字,字符串,布尔值,null,undefined

对象类型:对象,数组,函数(构造函数:用来初始化一个新建的对象)

类:数组类Array,函数类Function,日期类Date,正则类RegExp,错误类Error

转义字符:\

不可变的原始值和可变的对象引用

原始值是不可更改的,而对象类型又称为引用类型

var a=[];var b=a;b[0]=1;a[0]//=>1

null和undefined

null是关键字,表示空的值,typeof(null)//=>'object'

undefined是全局变量,表示未定义,typeof(undefined)//=>'undefined'

null==undefined//=>true

布尔值

除了undefined,null,0,-0,NaN,""转换为false

其他转换为true

正则

/^html///匹配html开头的字符串

var text="testing:1,2,3";

var pattern=/\d+/g;//匹配所有包含一个或多个数字的实例

pattern.test(text);//=>true;

text.search(pattern)//=>9:首次匹配成功的位置:从1开始

text.match(pattern);//=>["1","2","3"]

text.replace(pattern,"#");//=>"testing:#,#,#";

text.split(/\D+/);//["","1","2","3"]:用非数字字符截取字符串

字符串

var s="hello,world";

s.charAt(0)//=>"h";

s.charAt(s.length-1)//=>"d";

s.substring(1,4)//=>"ell";

s.slice(1,4)//=>"ell";

s.slice(-3)//=>"rld":最后三个字符

s.indexOf("l")//=>2:第一次出现的地方

s.lastIndexOf("l")//=>10

s.indexOf("l",3)//=>3:在位置3之后首次出现字符的位置

s.split(",")//=>["hello","world"]

s.replace("h","H")//=>"Hello,world"

s.toUpperCase()//=>"HELLO,WORLD"

​toString()

var n=17;

n.toString(2)//=>'10001'

n.toString(8)//=>21

n.toString(16)//=>11

({x;1,y:2}).toString()//=>'[object object]'

[1,2,3].toString()//=>'1,2,3'

new Date().toString()//=>"Sun Oct 27 2019 19:31:46 GMT+0800 (中国标准时间)"

/\d+/g.toString()//=>"/\d+/g"

(function(x){f(x);}).toString()//=>"function(x){f(x);}"

字符串是固定不变的

日期

var now=new Date();//=>2019.10.27 12:07:55

now.getFullYear()//=>2019

now.getMonth()//=>9;从0开始

now.getDate()//=>27

now.getDay()//0;0代表星期日,5代表星期一

now.getHours()//=>12

now.getMinutes()//=>7

now.getSeconds()//=>55

数字

所有数字都是浮点数,64位,整数范围:-2^53~2^53;无穷大:infinity;不是数字:NaN(用isNaN()判断)

整型:

十六进制:0x开头

八进制:0开头

格式

var n=123456.789;

n.toFixed(0)//=>123457

n.toExponential(3)//=>1.235e+5

n.toPrecision(7)//=>123456.8

n.toPrecision(10)//=>123456.7890

二进制浮点数和四舍五入错误

var x=.3-.2;

var y=.2-.1;

x==y//=>false

x==.1//=>false

y==.1//=>true

算术运算

- +-*/%

- Math.pow(n,m)//=>n^m

- Math.round()//=>四舍五入

- Math.ceil()//向上取整

- Math.floor()//向下取整

- Math.abs()//绝对值

- Math.max()//最大值

- Math.min()//最小值

- Math.random()//0-1.0的随机数

- Math.PI//圆周率

- Math.sqrt()//根号下

- Math.pow()//幂

全局对象

全局属性:undefined,infinity,NaN等

全局函数:isNaN(),parseInt(),eval()等

构造函数:Date(),RegExp(),String(),Object(),Array()等

全局对象:Math,JSON等

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

推荐阅读更多精彩内容