模拟new实现

function newObject() {
let obj = new Object();
let Con = [].shift.apply(arguments)
obj.__proto__ = Con.prototype;
let res = Con.apply(obj,arguments)
return typeof res == "object" ? res : obj;
}

模拟instanceOf

function instanceOf(left,right) {
let proto = left.__proto__;
let prototype = right.prototype
while(true) {
if(proto == null) return false
if(proto == prototype) return true
proto = proto.__proto__;
}
}

防抖 debounce

function debounce(fn,wait=50,immediate) {
let timer;
return function() {
if(immediate) {
fn.apply(this,arguments)
}
if(timer) clearTimeout(timer)
timer = setTimeout(()=> {
fn.apply(this,arguments)
},wait)
}
}

节流 throttle

function throttle(fn,wait=50) {
let timer;
return function() {
if(!timer) {
timer = setTimeout(()=> {
fn.apply(this,arguments)
timer = null
},wait)
}
}
}

jsonp

function jsonp(url,callback,success) {
let script = document.createElement("script")
script.src = url;
script.async = true;
script.type = "text/javascript"
window[callback] = function(data) {
success && success(data)
}
document.body.append(script)
}

继承

function a() {
this.a = "a"
}
a.prototype.test = function() {
console.log(this.a)
} function b() {
a.call(this);
this.b = "b"
} function tmp() {}
tmp.prototype = a.prototype;
b.prototype = new tmp();
b.prototype.construcotr = b;

模拟call

Function.prototype.mycall = function(content = window) {
content.fn = this;
let args = [...arguments].slice(1);
let result = content.fn(...args);
delect content.fn;
return result;
}

模拟apply

Function.prototype.myapply = function(content = window) {
content.fn = this;
let result; if(argument[1]) {
result = content.fn(...argument[1]);
} else {
result = content.fn();
}
delect content.fn;
return result;
}

模拟bind

Function.prototype.mybind = function(content) {
if(typeof this != "function") {
throw Error("not a function")
}
let fn = this;
let args = [...arguments].slice(1); let resFn = function() {
return fn.apply(this.instance == resFn ? this : content,args.concat(...arguments) )
}
function tmp() {}
tmp.prototype = this.prototype;
resFn.prototype = new tmp(); return resFn;
}

最新文章

  1. fir.im Weekly - 让 iOS 应用更加安全
  2. (转)android自定义组合控件
  3. Repository设计模式
  4. [实变函数]3.2 可测集 (measurable set)
  5. Java中的OutOfMemoryError的各种情况及解决和JVM内存结构
  6. override equals in Java
  7. VisualSVN Server的windows 2003配置和使用方法(图文并茂)
  8. javascript的本地存储 cookies、localStorage
  9. TheFourthJavaText
  10. Java开发速度神器Lombok,Eclipse端安装使用教程
  11. Python+Visual Studio
  12. Java经验杂谈(2.对Java多态的理解)
  13. Android学习(三)
  14. python 计算机发展史,线程Process使用 for循环创建 2种传参方式 jion方法 __main__的解释
  15. Info - 信息分析思路概要
  16. Windows中几个内存相当的指标
  17. react-navigation实现页面框架(转载)
  18. Python的可迭代对象、迭代器和生成器
  19. AI---训练集(train set) 验证集(validation set) 测试集(test set)
  20. AWS CSAA -- 04 AWS Object Storage and CDN - S3 Glacier and CloudFront(一)

热门文章

  1. 三次握手和四次挥手以及TCP标志位的详细介绍
  2. Ubuntu下使用git clone 的权限问题解决方法
  3. 如何判断一个List集合中是否有空值
  4. Facade——外观模式
  5. 2019牛客多校第五场F maximum clique 1 最大独立集
  6. Python 第三天学习整理①
  7. 六、SpringBoot配置@ConfigurationProperties与@Value区别
  8. mysql中级操作
  9. ivew-admin 点击预览图片
  10. 执行cython文件