一.对象
JavaScript中的所有属性键都是字符串,值都是任意对象!
Javascript的对象都是由若干个键值对组成
var 对象名{ 属性名:属性值, 属性名:属性值, 属性名:属性值 } //最后一个键值对不用加逗号 ,
<script> // 定义了一个person对象它有4个属性 var person = { name: "maming", age: 3, email: "1247238460@qq.com", score: 0 } </script>
js中对象,{.....}表示一个对象,键值对描述 属性XXX: XXXX;多个属性之间使用逗号隔开,最后一个属性不加逗号
1.对象赋值
person.name="mashang" 'mashang' person.name 'mashang'
2.在使用一个不存在的对象属性时,不会报错!==》undefined
person.niubi undefined
3.动态删减属性
delete person.name true person {age: 3, email: '1247238460@qq.com', score: 0}
通过delete删除对象的属性
4.动态添加属性
person.user="nihao" 'nihao' person {age: 3, email: '1247238460@qq.com', score: 0, user: 'nihao'}
5.判断属性值是否在这个对象中! XXX in XXX
‘age’ in person ture
// 继承思想 每个对象都会继承一个类
‘toString’ in person
ture
6.判断一个属性是否是这个对象自身拥有的 hasOwnProperty()
person.hasOwnProperty('toString') false person.hasOwnProperty('age') true
二.分支和循环详解(大部分和Java一致)
if判断
<script> "use strict"; let age =3 ; if(age > 18){ alert("成年"); }else { alert("未成年"); } </script>
循环
<script> "use strict"; let age = 36; while (age < 40){ age =age+1; console.log(age); } </script>
for循环
<script> "use strict"; let age = 36; for (let i = 0; i < 10; i++) { age = age+i; console.log(age); } </script>
forEach循环
<script> "use strict"; var age = [12,23,25,36,36,1,2,3]; //函数 function age.forEach(function (value) { console.log(value) }) </script>
for .. in
<script> "use strict"; var age = [12,23,25,36,36,1,2,3]; // for(var index(索引) in object) for (var num in age){ if(age.hasOwnProperty(num)){ console.log("存在"); console.log(age[num]); } } </script>
三.Map和Set
map和set在JavaScript的ES6特性,在其他版本是没有的
1.Map:集合
<script> "use strict"; //命题: 学生成绩:学生名字 // var name =["tom","jack","nima"]; // var score = [99,63,68]; var map=new Map([["tom",99],["jack",63],["nima",68]]); var name = map.get("tom"); console.log(name); map.set("admin",100); console.log(map); </script>
Map(4) [[Entries]] 0: {"tom" => 99} 1: {"jack" => 63} 2: {"nima" => 68} 3: {"admin" => 100} size: 4 [[Prototype]] : Map
2.Set:无续不重复的集合
<script> "use strict"; var set = new Set([3,3,2,4]); // set 可以去重 set.add(8); set.delete(1); console.log(set); </script>
Set(4)
[[Entries]] 0: 3 1: 2 2: 4 3: 8 size: 4
set.add()添加
set.delete()删除
四.Lterable迭代
通过 for .. of .. 直接可以遍历出值
而 for .. in .. 取的是下标
遍历数组
<script> "use strict"; var arr = [3,4,5]; for(var x of arr){ console.log(x);//打印下标for .. in .. } </script>
遍历map
<script> "use strict"; var map = new Map([["mm",18],["ll",96]]); for (let x of map) { console.log(x); } </script>
遍历Set
<script> "use strict"; var set = new Set([5,6,7]); for (let x of set) { console.log(x); } </script>