1. Promise模擬實現
2. Promise.all實現
Promise.all_ = function(promises) {
return new Promise((resolve, reject) => {
promises = Array.from(promises);
if(promises.length===0) {
resolve([]);
} else {
let result = [];
let index = 0;
for(let i=0; i<promises.length; i++) {
Promise.resolve(promises[i]).then(data => {
result[i] = data;
if(++index===promises.length) {
resolve(result);
}
}, err => {
reject(err);
return;
})
}
}
})
}
複製代碼
3.Promise.race實現
Promise.ra_ce = function(promises) {
promises = Array.from(promises);
return new Promise((resolve, reject) => {
if(promises.length===0) {
return;
} else {
for(let i=0; i<promises.length; i++) {
Promise.resolve(promises[i]).then(data => {
resolve(data);
return;
}, err => {
reject(err);
return;
})
}
}
})
}
複製代碼
4. call實現
Function.prototype.call2 = function (context) {
context = context || window;
context.fn = this;
var args = [];
for(var i = 1; i < arguments.length; i ++){
args.push('arguments['+i+']');
}
var result = eval('context.fn(' + args + ')');
delete context.fn;
return result;
}
複製代碼
5. apply實現
Function.prototype.apply2 = function(context,arr){
context = context || window;
context.fn = this;
var result;
if(!arr){
result = context.fn()
}else{
var args = [];
for(var i = 0; i < arr.length; i++){
args.push('arr[' + i + ']')
}
result = eval('context.fn(' + args + ')')
}
delete context.fn;
return result;
}
複製代碼
6. bind實現
Function.prototype.bind2 = function(context){
if(typeof this !== "function"){
throw new Error("")
}
var self = this;
var args = Array.prototype.slice.call(arguments,1);
var fNOP = function(){}
var fBound = function(){
var bindArgs = Array.prototype.slice.call(arguments);
self.apply(self instanceof fNOP ? this : context,args.concat(bindArgs));
}
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
}
複製代碼
7. 繼承方法
function Person1(name) {
this.name = name;
}
function Student1(name, age) {
Person1.call(this, name);
this.age = age;
}
var s1 = new Student1("xuhanlin");
console.log(s1.name);
function Person2() {
this.name = "renyuan";
}
function Student2(age) {
this.age = age;
}
Student2.prototype = new Person2();
var s2 = new Student2();
console.log(s2.name);
function Person3() {
this.name = "renyuan";
}
function Student3() {
Person3.call(this);
this.age = "18";
}
Student3.prototype = new Person3();
var s3 = new Student3();
console.log(s3.name, s3.age);
複製代碼
8.閉包
for(var i = 10; i > 0; i--){
(function(i){
setTimeout(() => {
console.log(i);
},(10-i)*1000)
})(i)
}
複製代碼
9.防抖
function debounce(func,wait){
var timeout;
return function(){
var context = this;
var args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function(){
func.apply(context,args);
},wait);
}
}
複製代碼
10.節流
function throttle(func,wait){
return function(){
var context = this;
var args = arguments;
var previous = 0;
var now = +new Date();
if(now - previous > wait){
func.apply(context,args);
previous = now;
}
}
}
function throttle(func,wait){
var timeout;
return function(){
var context = this;
var args = arguments;
if(!timeout){
timeout = setTimeout(function(){
timeout = null;
func.apply(context,args)
},wait);
}
}
}
複製代碼
11.new實現
function create(Con, ...args) {
let obj = {}
Object.setPrototypeOf(obj, Con.prototype)
let result = Con.apply(obj, args)
return result instanceof Object ? result : obj
}
複製代碼
12.數組去重
var array = [1,1,'1'];
function unique(arr){
var res = [];
for(let item of arr){
if(res.indexOf(item) === -1){
res.push(item);
}
}
return res;
}
function unique(arr){
var res = [];
var newArr = arr.sort();
for(let i=0; i<newArr.length; i++){
if(newArr[i] !== newArr[i+1]){
res.push(newArr[i]);
}
}
return res;
}
function unique(arr){
return Array.from(new Set(arr));
}
function unique (arr) {
const map = new Map()
return arr.filter((a) => !map.has(a) && map.set(a, 1))
}
console.log(unique(array));
複製代碼
13.數組扁平化
var arr = [1, [2, [3, 4]]];
function flatten(arr) {
var result = [];
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
result = result.concat(flatten(arr[i]));
} else {
result.push(arr[i])
}
}
return result;
}
function flatten(arr) {
return arr.reduce(function (pre, item) {
return pre.concat(Array.isArray(item) ? flatten(item) : item)
}, []);
}
function flatten(arr) {
return arr.toString().split(",").map(function (item) {
return +item;
})
}
console.log(flatten(arr));
複製代碼
14.柯里化
15.快速排序
var quickSort = function(arr){
if(arr.length <= 1){
return arr;
}
const pivotIndex = Math.floor(arr.length / 2);
const pivot = arr.splice(pivotIndex,1)[0];
const left = [];
const right = [];
for(let i=0; i<arr.length; i++){
if(arr[i] < pivot){
left.push(arr[i]);
}else{
right.push(arr[i]);
}
}
return quickSort(left).concat([pivot], quickSort(right));
}
const arr = [98, 42, 25, 54, 15, 3, 25, 72, 41, 10, 121];
console.log(quickSort(arr));
複製代碼
16.冒泡排序
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
const temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
var arr = [10, 14, 8, 5, 88];
console.log(bubbleSort(arr));
複製代碼
17.深淺拷貝
var shallowCopy = function(obj) {
if (typeof obj !== 'object') return;
var newObj = obj instanceof Array ? [] : {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = obj[key];
}
}
return newObj;
}
var deepCopy = function(obj){
if(typeof(obj) !== "object") return;
var newObj = obj instanceof Array ? [] : {};
for(let key in obj){
if(obj.hasOwnProperty(key)){
newObj[key] = typeof obj[key] === "object" ? deepCopy(obj[key]) : obj[key];
}
}
return newObj;
}
複製代碼