变量提升: 先用变量,在定义
列如:console.log(msg);
var msg=“helloworld”;
null返回的是object
函数返回的是function
typeof 返回当前的数据类型
undefined和null相等
NaN 非数值:是一个特殊的值
如:将字符串解析成数字出错的场合
NaN与任何数运算都是NaN
数组,对象,window,null 返回的都是object
instanceof 判断一个变量是否某个对象的实例
=== 可以判断数值和数据类型
==判断数值
1==“1” true
1===“1” false
parseInt()和parseFloat()
parseInt() 1.把值转换成整数 ,2.parseInt(“AF”, 16); 把字符串进制数转换为整数
parseFloat()把值转换成浮点数
这两个方法只对String类型有用 ,其他类型返回NaN
toSting()把内容转换为字符串, console.log( toString(1)) ; //“1”
toFixed()函数根据小数点后指定位数将数字转为字符串 ,四舍五入
var a=1.24;
console.log(a.toFixed(0)) //1
console.log(a.toFixed(0)) //2
********************数组
定义数组
1.var a=[值1,值2…];
2. var arr =new Array (1,2,3,4,5,7);
3. var a1=new Array(5); //赋值: a1[0]=2, a1[1]=3;
遍历数组
写个例子
var s1=[1,“r”,2,5,7,8,“t”];
var len=s1.length;
1.
for(var i=0;i<len;i++){
console.log(s1[i]);
}
- for(var i in s1){
console.log(s1[i]);
}
push: t添加元素到最后
console.log(s1.push(4)); //[1,“r”,2,5,7,8,“t”,4];
unshift 添加元素到最前面
pop删除最后一个元素
shift 删除第一个元素
reverse 反转
s1.indexof(t) //6 根据元素获取索引
*******************************函数
注意:::::::>块没有作用域,函数有作用域,
1.定义在函数内的变量:局部变量,定义在函数外的是全局变量,
2.如果在函数内没有用var关键字定义变量,则这个变量为全局变量
3.外部不能方法局部变量的,想要获取函数内部的局部变量—>闭包 但是需要两个函数
///闭包1.得到函数内部的局部变量,2.变量的值始终保存在内存中,3.避免变量冲突问题
function tt(){
var s="helloworld";
function h(){
console.log(s)
}
return h;
}
var sh=tt();
sh();
函数的创建
例子:
1.
function ff(x){
console.log(“哈哈哈”);
}
ff(1);
test();
function test(){
console.log(1);
}
var add =new function(形参, 函数体的语句 )
add(x,y);
函数提升: 先调用,后声明;
return 1.结束方法, 将值返回调用者;
函数调用:
1.
function aa(a,b){
return a+b;
}
var sum=add(2,4);
console.log(sum);
var o={
h:function(a,b){
retrun a+b;
}
}
o.h(5,6);
-
var obj={};
function sum(a,b){
return a+b;
}console.log(sum.call(obj,2,5));
console.log(sum.call(obj,[6,7]);
匿名函数调用
(funtion (a,b){
return a+b;
})(2,4)
arguments 对象中存放了调用该函数时传递的实际参数。
由于 JavaScript 允许函数有不 定数目的参数,所以我们需要一种机制,
可以在函数体内部读取所有参数,这就是 arguments 对象的由来 。
函数中可以使用 arguments 对象来获取实参
********对象
indexof(Chr):返回指定字符串的位置,从左到右.找不到返回-1
substr(m,n) 从m位置开始,取n个字符 ,找不到-1
substring(m,n) 从m位置开始,到n位置结束
getFullYear()年,getMonth()月,getDate()日,getHours()时,getMinutes()分,getSceonds()秒
1.getMonth():得到的值:0~11(1 月~12 月)
2.setMonth():设置值时 0~11
3.toLocaleString():可根据本地时间把 Date 对象转换为字符串,并返回结果。
创建一个对象
1.
var a={“name”:“哈哈”,“age”};
var a=new Object();
a.name=‘jv’;
a.get=19;
console.log(a);
var a=Object.create(null ); // a//可以是其他的模板函数
a.name=‘hr’
a.age=19
console.log(a)
对象的序列化,反序列化
序列化:将对象转为json字符串: JSON.stringify(object)
反序列化: 将Json字符串转为对象: JSON.parse(JsonStr)
eval ()1.将参数字符串解析成js代码执行 2.将json串转为json对象
eval(“console.log(‘哈喽’)”);//控制台输出哈喽
this 在对象里指代上级对象,
this 在函数里,指调用它的那个 window