<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
<script type="text/javascript">
// constructor 属性返回对创建此对象的数组函数的引用
// object.constructor
var test = new Array();
if(test.constructor == Array){
console.log('this is an Array');
}
if(test.constructor == Boolean){
console.log('this is a Boolean');
}
if(test.constructor == String){
console.log('this is a String');
}
if(test.constructor == Object){
console.log('this is a Object');
}
if(test.constructor == Date){
console.log('this si a Date');
}
function employee(name,job,born){
this.name = name;
this.job = job;
this.born = born;
}
var bill = new employee('Bill Gate',"Engineer",1985);
console.log(bill.constructor);
//
var arr = new Array(3);
arr[0] = 'Johh';
arr[1] = 'Andy';
arr[2] = "Wendy";
console.log('Original length' + arr.length)
arr.length = 5;
console.log("New length:" + arr.length);
// protoype 属性是您有能力向对象添加属性和方法
//object.prototype.name = value;
function employee(name,job,born){
this.name = name;
this.job = job;
this.born = born;
}
var bill = new employee('Bill Gate','Engineer',1985)
employee.prototype.salary = null;
bill.salary = 20000;
console.log(bill.salary); // undefine
/* contact() 方法用于连接两个或多个数组 返回一个新数组 原数组未改变 */
var a = [1,2,3];
console.log(a.concat(4,5));
// 创建两个数组 然后用concat把他们连接起来
var arr = new Array(3);
arr[0] = 'George';
arr[1] = 'John';
arr[2] = 'Thoms';
var arr2 = new Array(3);
arr2[1] = "James";
arr2[2] = "Adrew";
arr2[0] = "Martin";
console.log(arr.concat(arr2));
// 连接三个数组
var arr3 = new Array(2);
arr3[0] = "William";
arr3[1] = "Franklin";
console.log(arr.concat(arr2,arr3));
console.log(arr);
// join()方法用于把数组中的所有元素放入一个字符串中 原数组未改变
var arr = new Array(3);
arr[0] = "zjm";
arr[1] = 'cpf';
arr[2] = 'Tomas';
var chart = arr.join('---');
console.log(chart);
console.log(arr);
if(chart.constructor == String){
console.log('转换字符串成功');
}
//pop 方法用于删除并返回最后一个元素
var arr = new Array(3);
arr[0] = 'George';
arr[1] = "John";
arr[2] = "Tomas";
var pop = arr.pop();
console.log(pop);
console.log(arr); //原数组改变
//push() 方法想数组的末尾添加一个或多个元素,并返回新长度
var arr = new Array(3);
arr[0] = 'George';
arr[1] = "John";
arr[2] = "Tomas";
var push = arr.push('James');
console.log(arr); //原数组改变
console.log(push);
// reverse() 方法颠倒数组中元素的顺序
var arr = [ "George","Tomas","jim"];
console.log(arr.reverse());
console.log(arr); //该方法会改变原来的数组 而不会创建新的数组 pop push
// shift 删除并返回数组的第一个元素 改变原数组
console.log(arr.shift());
console.log(arr);
// unshift 插入倒数组的一个元素,并返回长度
var arr = [1,2,3];
console.log(arr.unshift(4));
console.log(arr);
//改变数组 push pop shift unshift splice
// slice 方法可从选定的已有的数组中返回选定的元素 不会改变原来的数组
var arr = [3,4,5,7,5];
console.log(arr.slice(1,3));
console.log(arr);
//sort 排序
var arr = [1,3,634,6,43,634,4545,];
//需要一个排序函数
function sortNam(a,b){ //从小到大的排列
return a - b;
}
console.log(arr.sort(sortNam));
//splice方法向数组中添加 删除项目 ,然后返回被删除的项目
//splice(a,num,add); a 开始的位置 num 删除的数量 add 增加的元素
var arr = ['zjm','jg','jw','jk','pf'];
console.log(arr.splice(1,2,'cl')); //插到删除的部分
console.log(arr);
//toString() 将数组返回字符串
// join()方法用于把数组中的所有元素放入一个字符串中 原数组未改变
var arr = ['Tomas','jim','ccy'];
console.log(arr.toString());
console.log(arr);
//value of 方法返回Array对象的原始值
</script>
</html>
请使用手机"扫一扫"x