JavaScript基础知识-对象Array

2019-06-25  本文已影响0人  aeborah

一.数组Array

1.创建数组

new Array();
new Array(size);

当其他参数调用 Array() 时,该构造函数将用参数指定的值初始化数组,当把构造函数作为函数调用,不使用 new 运算符时,它的行为与使用 new 运算符调用它时的行为完全一样。

2.对象属性
var test= new Array();
if(test.constructor==Array){
document.write("This is a Array");
}
if(test.constructor==Date){
document.write("This is a Date");
}
var arr= new Array();
document.write(arr.length);
}
<script type="text/javascript">
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
employee.prototype.salary=null;
bill.salary=20000;
document.write(bill.salary);
</script>
3.对象方法
var a = [1,2,3];
document.write(a.concat(4,5));
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr.join("."))

输出:George.John.Thomas
直接使用arr.join()时,默认分隔符为逗号

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr)
document.write(arr.pop())
document.write(arr)

输出:
George,John,Thomas
Thomas
George,John

start 必需。如果是负数,那么它规定从数组尾部开始算起的位置。也就是说,-1 指最后一个元素,-2 指倒数第二个元素,以此类推。
end 可选。该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从 start 到数组结束的所有元素。如果这个参数是负数,那么它规定的是从数组尾部开始算起的元素。

.sort((itemA, itemB) => {
        return itemA.stockinDate > itemB.stockinDate ? 1 : -1
      })
arr.splice(2,0,"William")
上一篇 下一篇

猜你喜欢

热点阅读