js数组操作

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>

 

posted @ 2017-09-05 19:26  alan-alan  阅读(147)  评论(0编辑  收藏  举报