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);