js數組操作
阿新 • • 發佈:2017-09-05
轉字符串 .so 負數 function 排序 array concat rip nbsp
1.js數組操作
<script> var str =‘hello‘; var arr =[‘hello‘,‘1‘,‘2‘];//字面量 var arr1 = 1; // console.log(str); // console.log(‘str‘); // console.log(typeof(str)); // console.log(arr.isArray); // 如果是負數(-1),那麽它規定從數組尾部開始算起的位置 // console.log(arr1.slice(-1));//[1,2] // console.log(arr instanceof Array)//true// console.log(Array.isArray(arr1))//true var test=[]; test.push(‘hello‘); test.push(‘wold‘); test.push(1); test.pop(); // console.log(‘after pop‘); // console.log(test); test.shift(); // console.log(‘after shift‘); // console.log(test); // 數組的截取和合並 test.push(‘hello1‘); test.push(‘wold2‘); // console.log(test.slice(-1)); var test2 =[1,‘2‘,‘str‘]; // console.log(test.concat(test2)); // 數組的拷貝 // console.log(test.slice(0)); // console.log(test2.concat()); // 數組元素的排序 // console.log(test2.reverse()); // console.log(test2.sort()); // 數組轉字符串化 // var strr = test2.join(‘@‘);// console.log(strr); // var arrr = strr.split(‘@‘); // console.log(arrr); console.log(test.length); // prototype var testm = function(){}; testm.prototype.fn= { hello:function(msg){alert(msg)} }; var testm = new testm(); console.log(testm); console.log(testm.fn); testm.fn.hello(‘hello‘); </script>
js數組操作