一、常量
不允许重复定义
const a=‘HELLO‘ const a=‘world‘//报错Uncaught SyntaxError: Identifier ‘a‘ has already been declared
const a=‘HELLO‘ a=‘world‘//Uncaught TypeError: Assignment to constant variable.
二 let使用
let:定义一个块级作用域的变量
需要先定义再使用;(不存在变量提升)
不能重复定义
普通变量(var 定义的):
这叫做变量提升:先使用变量,然后在定义变量
console.log(a);//不报错 undefined var a=100
相当于
var b; console.log(b);
b=100; console.log(b)
let定义的(Es6新增)
不存在变量提升
console.log(c);//报错:Uncaught ReferenceError: c is not defined let c=10;
let是一个块级作用域的变量:
let c = 100; if(10> 9){ let c=200; console.log(c); } console.log(c); // var c = 300
结果:200,100
var 定义的话
var c = 100; if(10> 9){ var c=200; console.log(c); } onsole.log(c);
结果:200,200
var i=10; var arr = [22,33,44,55] for(let i=0;i< arr.length;i++){ } if(i>9){ console.log(i+10); }
结果:20
三,js基本数据类型
js的数据类型: string array number null undefined boolean object 基本数据类型:string number null undefined boolean 引用类型:array object
const obj = { name: "谢小二", age: 22 } var obj2 = obj; obj2.age = 90 console.log(obj.age);
结果:90
基本数据类型引用只是引用了值,改变互不影响 a=10 b=a b=20 a根本没变 而引用类型的引用引用的是内存地址,一个改变另一个也跟的改变,如上述代码
四 模板字符串
通过反引号来使用,字符串当中可以使用变量
可以当作普通字符串来处理
可以使用多行字符串
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
</head>
<body>
<ul id="ul">
</ul>
</body>
<script>
var name=‘陈太章‘
console.log(`我的名字叫${name}`)
document.getElementById(‘ul‘).innerHTML=`
<li>赵俊明喜欢搞基</li>
<li>肖博雅喜欢被搞</li>
<li>路宁喜欢我给他戴帽子</li>
`
</script>
</html>
五 解构变量
类型要一致,如果是对象的话字段名称要一致
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<script>
// let [a,b,c,d]=[11,22,33,44];
// console.log(a);//11
// console.log(d);//44
// let [a,b,c,[d]] = [89,90,99,[100]];
// console.log(a);//89
// console.log(c);//99
// console.log(d);//100
let obj={
name:‘aaa‘,
sex:‘male‘
}
let {name,sex}=obj;
console.log(name);//aaa
console.log(sex);//male
</script>
</head>
<body>
</body>
</html>
六对象的扩展
对象的扩展
对象当中的属性可以简写
对象当中的方法也可以简写
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<script>
var username = ‘长安大学‘;
var addr = ‘陕西西安‘;
function fun() {
alert(‘it is very beautiful‘)
}
//注意字段要保持一致
var obj = {
username,
addr,
fun,
};
console.log(obj.username);
obj.fun()
// var useranme = $("#text1").val();
// var password = $("#text2").val();
// $.get(url,{useranme,password},function(){
//
//
// })
</script>
</head>
<body>
</body>
</html>
七、函数的扩展
函数的扩展 可以给函数默认参数 剩余参数:function fun(a,...b ){ } fun(11,22,33) 则:b = [22,33]
默认参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<script>
function aa(name=‘渣渣辉‘) {
console.log(name)
}
aa()
</script>
</head>
<body>
</body>
</html>
剩余参数:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<script>
// function aa(name=‘渣渣辉‘) {
// console.log(name)
// }
// aa()
function bb(a,...b) {
console.log(a);
console.log(b);
}
bb(‘渣渣辉‘,‘古天乐‘,‘陈小春‘,‘贪玩蓝月‘)
</script>
</head>
<body>
</body>
</html>
结果:
a=渣渣辉
b=["古天乐","陈小春","贪玩蓝月"]
八、数组的扩展
1)判断数组当中是否存在某个数值
indexOf 有就找出索引,没有则输出-1
<script> aa=[11,22,33,44,55,66]; console.log(aa.indexOf(55));//4 console.log(aa.indexOf(45))//-1 </script>
includes 有则显示true没有则显示false
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<script>
aa=[11,22,33,44,55,66];
// console.log(aa.indexOf(55));
// console.log(aa.indexOf(45))
console.log(aa.includes(55));//true
console.log(aa.includes(45))//false
</script>
</head>
<body>
</body>
</html>
2)对数组的遍历
aa=[11,22,33,44,55,66]; aa.forEach(function (item,index) { console.log(item,‘----‘,index) })
3)map操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<script>
aa=[11,22,33,44,55,66];
// console.log(aa.indexOf(55));
// console.log(aa.indexOf(45))
//
// console.log(aa.includes(55));//true
// console.log(aa.includes(45))//false
//
// aa.forEach(function (item,index) {
// console.log(item,‘----‘,index)
// })
arr=aa.map(function (item,index) {
return item+1
});
console.log(arr)//[12,23,34,45,56,67]
</script>
</head>
<body>
</body>
</html>
4)对数组的过滤
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<script>
aa=[11,22,33,44,55,66];
// console.log(aa.indexOf(55));
// console.log(aa.indexOf(45))
//
// console.log(aa.includes(55));//true
// console.log(aa.includes(45))//false
//
// aa.forEach(function (item,index) {
// console.log(item,‘----‘,index)
// })
// arr=aa.map(function (item,index) {
// return item+1
// });
// console.log(arr)//[12,23,34,45,56,67]
arr1=aa.filter(function (item,index) {
return item>50
});
console.log(arr1)//[55,56]
</script>
</head>
<body>
</body>
</html>
九、类
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<script>
Object.prototype.hobby=‘play game‘;
function Person(name,age) {
this.name=name;
this.age=age;
this.sayhello=function () {
console.log(‘hello‘)
}
}
Person.prototype.sayhello=function () {
console.log(‘哈哈哈哈‘)
};
Person.prototype.addr=‘北京‘;
var p=new Person(‘ctz‘,21);
p.sayhello();
console.log(p.addr);
console.log(p.hobby)
</script>
</head>
<body>
</body>
</html>
hello
北京
play game
大佬地址链接:
github:
https://github.com/ruanyf/es6tutorial/tree/gh-pages/docs