JavaScript
对象
对象的定义
var 对象名={
属性:属性值,
属性:属性值,
属性:属性值
}
(最后一个不需要逗号)
实例
var Person={
name:"lixiucheng",
age:34,
email:"2897420133@qq.com",
score:100
}
-
对象的赋值
Person.name="tangsan" "tangsan" Person.name "tangsan"
-
使用一个未定义的属性不是报错
Person.hhhh undefined
-
可以动态删除属性
delete Person.name true Person {age: 34, email: "2897420133@qq.com", score: 100}
-
动态的添加
Person.hhahah="hahhh" "hahhh" Person {age: 34, email: "2897420133@qq.com", score: 100, hhahah: "hahhh"}
-
JavaScript中所有键(对象属性)都是字符串,所有属性值可以任意!
Person["age"]
34
-
判断一个属性是否在对象中(xxxx in xxxx)
"age" in Person true "tt" in Person false
-
判断一个属性是否在这个对象中 hasOwnProperty( )
Person.hasOwnProperty("age") true Person.hasOwnProperty("toString") false
流程控制(基本和c语言一样)
if{}else{}
var age1=3;
if(age1>3){
alert("wuwuwu~");
}else{
alert("yingyingying~");
}
for
for (let i = 0; i <10 ; i++) {
console.log(i);
}
while
var i=10;
while(i<20){
console.log(i);
i++;
}
forEach数组循环
var arr=[12,34,34,56,23,78];
arr.forEach(function (value){
console.log(value);
})
Map和Set
//ES6 Map
var map=new Map([["tom",100],["jack",99]]);
var name1=map.get("tom");
map.set("ok",34);//新增
map.delete("tom");//删除
console.log(name1);
Map(3) {"tom" => 100, "jack" => 99, "ok" => 34}
//Set
var set=new Set([1,21,23,1,11,1,1]);
Set(4) {1, 21, 23, 11}//输出时会去掉重复值
set.add(90);//新增
Set(5) {1, 21, 23, 11, 90}
set.delete(1);//删除
Set(4) {21, 23, 11, 90}
console.log(set.has(90));//是否包含某个元素
true
iterator(遍历数组…)
//打印数组
var b=[23,34,45,67];
for (var x of b){
console.log(x);
}
23 34 45 67
=========================================================
//遍历Map
var map1=new Map([["li",990],["nihao",100],["ok",99]])
for(let x of map1){
console.log(x);
}
["li", 990]
["nihao", 100]
["ok", 99]
//遍历set
var set1=new Set([2,23,2,1]);
for(let x of set1){
console.log(x);
}
2,23,1