JavaScript 对象 ------Array对象用法

JavaScript 对象主要为:js Array , js Boolean , js Date , js math , js Number , js String , js RegExp , js Functions , js Envents.现在我们就来简单的介绍一下这些对象如何使用。

1:js Array

array 对象可以用于在单个对象中存多个值,创建array对象的语法是:

  • new Array();
  • new Array(size);
  • new Array{tmp1,tmp2,tmp3,.....}

array对象属性:

  • constructor 返回对创建此函数的引用

var test = new Array();

if(test.constructor == Array){

document.write("this is Array");

}

  • length 设置或返回数组函数的数目

var  test = new Array(2);

test[0] = "星期一";

test[1] = "星期二";

document.write("test Length : "+test.length);

test.length = 5;

document.write("test New Length : "+test.length);

  • prototype 为数组添加属性和方法

function information(name,job,age){

this.name = name;

this.job = job;

this.age = age;

}

var test = new information("李雷","java开发工程师",26);

test.prototype.salary = null;

test.salary = 8000;

test.prototype.sex = null;

           test.sex= "男";

         document.write("My name is "+test.name+",My sex is "+test.sex+",My job is "+test.job+",My salay is "+test.salay+"元 Can you be my gril frrend ?");

array 对象方法:

  • concat() 将两个或多个数组连接并返回结果

var test = new Array();// var test = ["星期一","星期二"];

test[0] = "星期一";

test[1] = "星期二";

document.write(test.concat("星期三","星期四"));

  • join() 将所有元素放到一个字符串,并以指定的分隔符分隔

var test = new Array();

test[0] = "星期一";

test[1] = "星期二";

test.concat("星期三","星期四");

document.write(test.join("."));

  • pop() 删除最后一个元素并返回,该方法会改变数组长度

var test = new Array();

test[0] = "星期一";

test[1] = "星期二";

test.concat("星期三","星期四");

document.write(test);

document.write(test.pop());

document.write(test);

  • push() 在末尾新增一个元素,并返回新的长度

var test = new Array();

test[0] = "星期一";

test[1] = "星期二";

test.concat("星期三","星期四");

document.write(test);

document.write(test.push("星期五"));

document.write(test); 

  • reverse() 颠倒元素顺序

var test = new Array();

test[0] = "星期一";

test[1] = "星期二";

test.concat("星期三","星期四");

document.write(test);

document.write(test.reverse());

  • shift() 删除并返回数组中的第一个元素

var test = new Array();

test[0] = "星期一";

test[1] = "星期二";

test.concat("星期三","星期四");

document.write(test);

document.write(test.shift());

document.write(test);

  • slice() 返回选定的元素

var test = new Array();

test[0] = "星期一";

test[1] = "星期二";

test.concat("星期三","星期四");

document.write(test);

document.write(test.slice(1,3));

document.write(test);

  • sort() 数组元素排序 单纯的objectArray.sort()只能按每个元素的第一位来排序,可以在objectArray.sort(Method)添加排序方法
  • splice() 删除元素,并在删除元素的地方添加新元素

var test = new Array();

test[0] = "星期一";

test[1] = "星期二";

test.concat("星期三","星期四");

document.write(test);

document.write(test.splice(1,2,"星期五"));//注意第一个参数是从哪个位置开始删除,第二个参数是删除的位置开始删除几个元素,第三个参数是在删除的地方添加的新元素

document.write(test);

  • toString() 将数组元素转换成字符串

var test = new Array();

test[0] = "星期一";

test[1] = "星期二";

test.concat("星期三","星期四");

document.write(test.toString());

  • unshift() 在数组开始位置添加一个或多个元素,并返回新的长度

var test = new Array();

test[0] = "星期一";

test[1] = "星期二";

test.concat("星期三","星期四");

document.write(test);

document.write(test.unshift("星期六","星期日"));

document.write(test);

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值