promise和高阶函数
promise
高阶函数
//原型: 每个函数自带的一个属性,属性名称叫做 prototype,
// 它的值是一个对象,让构造函数的属性和方法,进行共享
//减少内存的开销
// toString() 每个构造函数都有toString()方法
// Date.prototype.toString=function(){
// console.log("帅哥在台上");
// }
// var date = new Date();
// console.log(date);
// Array.prototype.toString=function(){
// console.log("222");
// }
// var arr=[11,22,33];
// console.log(arr.toString());
// console.log( Array.prototype);
// var arr1=[11,22,33];
// var arr2=[11,22,33];
// forEach,map,some,every,filter,reduce,sort
// console.log( Array.prototype.sort);
//1).forEach
Array.prototype.myforEach = function (callback) {
//this, 谁调用myforEach,this就是谁!
console.log(this);
for (var i = 0; i < this.length; i++) {
callback(this[i], i, this);
}
}
var arr1 = [1, 6, 3, 7, 9];
arr1.myforEach(function (item, index, arr) {
console.log(item, index, arr);
})
//2).map
Array.prototype.myMap = function (callback) {
var list = [];
for (var i = 0; i < this.length; i++) {
list.push(callback(this[i], i, this))
}
return list
}
var arr1 = [1, 6, 3, 7, 9];
var res = arr1.myMap(function (item, index, arr) {
return item * 10
})
console.log(res);
//3).你自己写一个mysort方法
Array.prototype.mySort = function (callback) {
for (var i = 0; i < this.length - 1; i++) {
for (var j = 0; j < this.length - 1 - i; j++) {
if (callback(this[j], this[j + 1]) >= 1) {
var temp = this[j + 1];
this[j + 1] = this[j];
this[j] = temp;
}
}
}
return this;
}
var arr1 = [1, 6, 3, 7, 9];
var res = arr1.mySort(function (a, b) {
return a - b;
})
console.log(res);
//4).filter
Array.prototype.myfilter = function (callback) {
var list = [];
for (var i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
list.push(this[i])
}
}
return list;
}
var arr1 = [1, 6, 3, 7, 9];
var res = arr1.myfilter(function (item, index, arr) {
return item > 5
})
console.log(res);
//5).some
Array.prototype.mySome = function (callback) {
for (var i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
return true;
}
}
return false;
}
var arr1 = [1, 6, 3, 7, 9];
console.log(arr1.mySome((item, index, aaa) => {
return item > 2
}));
//6).every
Array.prototype.myevery = function (callback) {
var flag = true;
for (var i = 0; i < this.length; i++) {
if (!callback(this[i], i, this)) {
flag = false;
break;
}
}
return flag
}
var arr1 = [1, 6, 3, 7, 9];
console.log(arr1.myevery((item, index, aaa) => {
return item > 2
}));
//reduce
// Array.prototype.Myreduce = function(callback,f){
// var str
// if(f||''==f){
// str = f
// var i = 0
// }
// else{
// str = this[0]
// var i = 1
// }
// for(;i
// }
// return str
// }
// console.log(arr1.Myreduce(function(pre,cur){
// return pre+cur
// },''));
//1
//var arr1 = [1, 6, 3, 7, 9];
Array.prototype.myreduce = function (callback, init) {
//如果一个参数,pre就是第一个数,下标从1开始
if (arguments.length == 1) {
var pre = this[0];
for (var i = 1; i < this.length; i++) {
var cur = this[i];
//把回调函数的返回值丢给下一次的pre
pre = callback(pre, cur, i, this);
}
return pre;
}
//如果2个参数,pre就是第初始值,下标从0开始
if (arguments.length == 2) {
var pre = init;
for (var i = 0; i < this.length; i++) {
var cur = this[i];
//把回调函数的返回值丢给下一次的pre
pre = callback(pre, cur, i, this);
}
return pre;
}
// var pre = arguments.length == 2 ? init : this[0];
// var i = arguments.length == 2 ? 0 : 1;
// for (; i < this.length; i++) {
// var cur = this[i];
// pre = callback(pre, cur, i, this);
// }
// return pre;
}
console.log("-----------------");
// var arr1 = [1, 6, 3, 7, 9];
// console.log(arr1);
// //1个参数 5个数 4次
arr1.myreduce(function (pre, cur, index, arr) {
// 1 6
// undefined 3
console.log(pre, cur, index);
})
console.log("-----------------");
// //2个参数 5个数 5次
arr1.myreduce(function (pre, cur, index, arr) {
console.log(pre, cur, index);
}, "你好")