首页 > Web开发 > 详细

js笔记

时间:2016-12-13 19:00:48      阅读:232      评论:0      收藏:0      [点我收藏+]

1.克隆对象

克隆数组:

var country=[‘中国‘,‘美国‘];
var copyCountry=country.slice(0);

克隆对象:

var people={sex:‘man‘,age:4};
var me=JSON.parse(JSON.stringify(people));

2.随机数

从数组中随机取n个不重复的元素

 function getRandomArrayElements(arr, count) {
        var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
        while (i-- > min) {
            index = Math.floor((i + 1) * Math.random());
            temp = shuffled[index];
            shuffled[index] = shuffled[i];
            shuffled[i] = temp;
        }
        return shuffled.slice(min);
    }

 

js笔记

原文:http://www.cnblogs.com/CallmeYhz/p/6170815.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!