# call apply bind
thisArg = thisArg == undefined ? window : thisArg;
let type = typeof thisArg;
if (!/^(object|function)$/.test(type)) {
if (/^(symbol|bigint)$/) {
thisArg = Object(thisArg);
} else {
thisArg = new thisArg.constructor(thisArg);
}
}
~function anonymous (proto){
function call (context=window,...args){
context === null ? context = window : null
let type = typeof(context)
if (type !== "object" && type !== "function" && type !== "symbol") {
//=>基本类型值
switch (type) {
case 'number':
context = new Number(context);
break;
case 'string':
context = new String(context);
break;
case 'boolean':
context = new Boolean(context);
break;
}
}
context.$fn = this
let result = context.$fn(...args)
delete context.$fn
return result
}
function apply(context,args){
context.fn = this
let result = context.fn(...args)
delete context.fn
return result
}
function bind(context=window,...args){
return (...amArgs) = >{
this.call(context,...args.concat(amArgs))
}
}
function bind(context){
context = context || window
var args = [].slice.call(arguements,1)
var _this = this
return function(){
var amArgs = [].slice.call(arguements,0)
_this.apply(context,args.concat(amArgs))
}
}
proto.call = call
proto.apply = apply
proto.bind = bind
}(Function.prototype)
# new
function new(Func,...args){
let obj = Object.create(Func.prototype)
let result = Func.call(obj,...args)
if(typeof result === "object" && result !== null || typeof result === "function"){
return result
}
return obj
}
# 原型继承
function Parent(){
....
}
functon Child(){
....
}
Child.prototype = new Parent
Child.prototype.getY = function getY(){}
# call继承
function Parent(){
this.x = 100
}
functon Child(){
Parent.call(this)
}
# 寄生组合
function Parent(){
this.x = 100
}
functon Child(){
Parent.call(this)
}
Child.prototype = Object.create(Parent.prototype)
Child.prototype.constructor = Child
# instaceof
function instanceof(left,right){
let prototype = right.prototype
let proto = L._proto
while(true){
if(proto === null) return false
if(proto === prototype) return true
proto = proto._proto_
}
}
# Object.create
function create(obj){
function F(){
F.prototype = obj
}
return new F()
}
# 防抖
在事件被触发n秒后在执行回调,如果在这n秒内又被触发,则重新计时。
function debounce(fn,delay){
let timer = null
return function(){
clearTimeout(timer)
timer = setTimeout(function(){
fn.apply(this)
},delay)
}
}
# 节流
function throttle(fn,delay){
var lastTime = 0
return function(){
var nowTime = Date.now()
if(nowTime - lastTime > delay){
fn.call(this)
lastTime = nowTime
}
}
}
# Object.assign
Object.defineProperty(Object,"assign",{
value:function(target){
if(target==null){
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target)
for(var index=1;index<arguements.length;index++){
var nextsource = arguements[index]
if(nextsource!=null){
for (var nextkey in nextsource){
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)){
to[nextkey] = nextSource[nextKey];
}
}
}
}
return to
},
writable:true,
configurable:true
})
# 深拷贝
function deepclone(obj){
if(obj===null) return null
if (typeof obj !== "object") return obj;
if (obj instanceof RegExp) return new RegExp(obj);
if (obj instanceof Date) return new Date(obj);
let cloneObj = new obj.constructor;
for (let key in obj) {
if (!obj.hasOwnProperty(key)) break;
cloneObj[key] = deepclone(obj[key]);
}
return cloneObj;
}
# 数组去重
for (let i = 0; i < arr.length - 1; i++) {
let item = arr[i];
for (let j = i + 1; j < arr.length; j++) {
if (item === arr[j]) {
arr[j] = arr[arr.length - 1];
arr.length--;
j--;
}
}
}
let obj = {};
for (let i = 0; i < arr.length; i++) {
let item = arr[i];
if (obj[item] !== undefined) {
arr[i] = arr[arr.length - 1];
arr.length--;
i--;
continue;
}
obj[item] = item;
}
Array.from(new Set(arr));
let newAry=[]
for(let i=0;i<arr.length;i++){
let item=arr[i];
if(newAry.indexOf(item)==-1){
newAry.push(item);
}
}
# 数组最大最小值
ary.sort(function (a, b) {
return a - b;
});
let min = ary[0];
let max = ary[ary.length - 1];
let min = Math.min.apply(null,ary);