Function.prototype.mycall = function(context){
if(typeof this !== "function"){
console.error("type error");
}
let args = [...arguments].slice(1);
result = null;
context = context || window;
context.fn = this;
result = context.fn(...args);
delete context.fn;
return result;
}
Function.prototype.myBind = function(context){
if(typeof this !== "function"){
throw new TypeError("error");
}
var args = [...arguments].slice(1);
fn = this;
return function Fn(){
return fn.apply(
this instanceof Fn ? this : context,
args.concat(...arguments)
)
}
}
Function.prototype.myApply = function(context){
if(typeof this !== "function"){
throw new TypeError("error!")
}
let result = null;
context = context || window;
context.fn = this;
if(arguments[1]){
result = context.fn(...arguments[1]);
}else{
result = context.fn()
}
delete context.fn;
return result;
}
const SERVER_URL = "/server";
let xhr = new XMLHttpRequest();
xhr.open("GET", SERVER_URL, true);
xhr.onreadystatechange = function(){
if(this.readyState !== 4){
return;
}
if(this.readyState === 200){
handle(this.response)
}else{
console.error(this.statusText)
}
xhr.onerror = function(){
console.log(this.statusText);
}
xhr.responseType = "json";
xhr.setRequestHeader("Accept","application/json");
xhr.send(null);
}
let obj1 = {
a:0,
b:{
c:0
}
}
let obj2 = JSON.parse(JSON.stringify(obj1));
obj1.a = 1;
obj1.b.c = 1;
console.log(obj1);
console.log(obj2);
var arr = [1,2,3,4,5,6,7,8,9,10]
for(var i=0;i<arr.length;i++){
const randomIndex = Math.round(Math.random() * (arr.length - 1 - i)) + i;
[arr[i], arr[randomIndex]] = [arr[randomIndex], arr[i]];
}
console.log(arr)
let arr = [1,[2,[3,4,5]]];
function flatton(arr){
let result = [];
for(let i=0;i<arr.length;i++){
if(Array.isArray(arr[i])){
result = result.concat(flatton(arr[i]));
}else{
result.push(arr[i])
}
}
return result
}
flatton(arr);
function flatton(arr){
return arr.reduce(function(prev, next){
return prev.concat(Array.isArray(next) ? flatton(next) : next)
},[])
}
const array = [1,2,3,4,5,6,7,8,8,8,7]
Array.from(new Set(array))
function uniqueArray(array){
let map = {};
let res = [];
for(var i = 0;i<array.length;i++){
if(!map.hasOwnProperty([array[i]])){
map[array[i]] = 1;
res.push(array[i])
}
}
return res;
}
function add(a){
return function(b){
return function(c){
return a+b+c
}
}
}
console.log(a+b+c)
var add = function(m){
var temp = function(n){
return add(m+n)
}
temp.toString = function(){
return m;
}
return temp;
}
console.log(add(3)(4)(5))
function add(...args){
return args.reduce((a,b) => a+b)
}
function curring(fn){
let args = [];
return function temp(...newArgs){
if(newArgs.length){
args = [
...args,
...newArgs
]
return temp
}else{
let val = fn.apply(this, args)
args = []
return val
}
}
}
function sum(){
let sunm = 0;
Array.prototype.forEach.call(arguments, function(item){
sum += item * 1;
})
}
function sum(...nums){
let sum = 0;
nums.forEach(function(item){
sum += item * 1;
})
return sum
}
console