js 声明数组和向数组中添加对象变量的简单实例


数组有四种定义的方式

使用构造函数:

var a = new Array();
var b = new Array(10);
var c = new Array("first", "second", "third");

或者数组直接量:

var d = ["first", "second", "third"];

扩展:

function ObjStory(id,biaoti,author,type) //声明对象
{
    this.ID = id;
    this.Biaoti= biaoti;
    this.Author= author;
    this.Type = type;
    

}

var arr = new Array();//声明数组,用来存储标题信息

var writer= new ObjStory(11,‘大家去看海',‘李大胆',‘文艺类');//声明对象
arr[0]=writer;//向集合中添加对象

另一种方法:

var Array=[];

Array.push(new ObjStory(12,‘大家去看海',‘李大胆',‘文艺类'));

Array.push(new ObjStory(14,‘大家去看天',‘李小胆',‘文艺类'));

以上这篇js 声明数组和向数组中添加对象变量的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持phpstudy。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3