//手写call
/*Function.prototype.myCall= function(obj,...arr){
obj.fn = this;
obj.fn(...arr);
delete obj.fn;
}
let csss = {
k:function(...arr){
let s="begin:";
arr.forEach((value)=> s+=value);
console.log(this.name+"2"+s)
}
}
let obj = {
name:"2",
}
csss.k.myCall(obj,1,2,3,5);*/
//手写apply
/*Function.prototype.myApply= function(obj,arr){
obj.fn = this;
obj.fn(arr);
}
let csss = {
k:function(arr){
let s="begin:";
arr.forEach((value)=> s+=value);
console.log(this.name+"2"+s+this.name)
}
}
let obj = {
name:"2",
}
csss.k.myApply(obj,[1,2,3,5]);*/
//手写bind
/*Function.prototype.myBind= function(obj=window,...arr){
obj.fn = this;
var ff =function(){
obj.fn(arr);
}
return ff;
}
let csss = {
k:function(...arr){
let s="begin:";
arr.forEach((value)=> s+=value);
console.log(this.name+"2"+s)
}
}
let obj = {
name:"2",
}
csss.k.myBind(obj,1,2,3,8)();*/
//json字符串和对象相互转化
/*var s = {
name :2,
fn : function(){
return "fn";
},
age:"5",
}
let tt = JSON.stringify(s);//对象转化为字符串
let ss = JSON.parse(tt);//字符串转化为对象
let kk = eval("("+tt+")")//字符串转化为对象
console.log(kk);*/
//对象的深拷贝:递归实现
/*let b = {bname:"b",
bage:"b111"};
let a = {
name:111,
test:b,
qq:[1,2,3,4,5],
}
let c = {};
function deepClone(sourceobj,targetobj){
let cloneObj = targetobj|| {};
for(key in sourceobj){
if(sourceobj[key] instanceof Object && sourceobj[key]!=null){
cloneObj[key] =
deepClone(sourceobj[key],cloneObj[key]);
}
if(sourceobj[key] instanceof Array){
cloneObj[key] = sourceobj[key].concat();
}
else{
cloneObj[key] = sourceobj[key];
}
}
return cloneObj;
}
deepClone(a,c);
a.test.bage = 2;
console.log(c.test.bage);
console.log(c.qq[4]);*/
/*let a ={
name:"2",
age:100,
toString:function(){
return 22;
},
}
let c = eval("("+JSON.stringify(a)+")");
a.name = "50";
console.log(a.name)*/
//一层深拷贝
/*let a ={name:22,}
let c = Object.assign({},a);*/
function rund(min,max,N){
let a = [];
if(N>max-min){
return null;
}
while(N--){
var temp = (Math.random()*(max-min))/0.1+min;
if(a.indexOf(temp)==-1){
a.push(temp)
}else{
N++;
}
}
console.log(a);
}
rund(1,5,2);
console