数组/对象

前端 0 562 0
发表于: 2020-02-24 15:48:58

简介: 暂无~

数组

  1. 创建数组
    1. var arr = new Array()
    2. var arr = []
  2. 索引数组
arr[0] = '张三'
arr[1] = 18
arr[2] = "男"
console.log(arr)    //["张三", 18, "男"]
  1. 关联数组
arr["name"] = '张三'
arr["age"] = 18
arr["sex"] = "男"
console.log(arr)    //[name: "张三", age: 18, sex: "男"]

对象

  1. 创建对象
person = new Object();
person.firstname = "John";
person.lastname = "Doe";
person.age = 50;
person.eyecolor = "blue";
console.log(person) //{firstname: "John", 
lastname: "Doe", age: 50, eyecolor: "blue"}
person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"}
console.log(person) //{firstname: "John", lastname: "Doe", age: 50, eyecolor: "blue"}
//使用对象构造器
function person(firstname,lastname,age,eyecolor){
    this.firstname=firstname;
    this.lastname=lastname;
    this.age=age;
    this.eyecolor=eyecolor;
}
myFather=new person("John","Doe",50,"blue");
myMother=new person("Tim","li",48,"white");
console.log(myFather)  //person {firstname: "John", lastname: "Doe", age: 50, eyecolor: "blue"}
console.log(myMother)  //person {firstname: "Tim", lastname: "li", age: 48, eyecolor: "white"}
JavaScript

最后更新于:2021-02-16 21:01:55

欢迎评论留言~
0/400
支持markdown
Comments | 0 条留言
按时间
按热度
目前还没有人留言~