模拟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;
}

JS模拟实现题目(new debounce throwee 等)的更多相关文章

  1. js模拟抛出球运动

    js练手之模拟水平抛球运动 -匀加速运动 -匀减速运动 模拟运动有些基本的思路,当前所在点的坐标,元素的长宽是多少,向右/向下运动x/y增加,向上/向左运动x/y减少,运动的路程是多少,用什么方程进行 ...

  2. Gremlins.js – 模拟用户随机操作的 JS 测试库

    Gremlins.js 是基于 JavaScript 编写的 Monkey 测试库,支持 Node.js 平台和浏览器中使用.Gremlins.js 随机模拟用户操作:单击窗口中的任意位置,在表格中输 ...

  3. JS 模拟手机页面文件的下拉刷新

    js 模拟手机页面文件的下拉刷新初探 老总说需要这个功能,好吧那就看看相关的东西呗 最后弄出了一个简单的下拉刷新页面的形式,还不算太复杂 查看 demo 要在仿真器下才能看到效果,比如chrome的里 ...

  4. 由chrome剪贴板问题研究到了js模拟鼠标键盘事件

    写在前面 最近公司在搞浏览器兼容的事情,所有浏览器兼容的问题不得不一个人包了.下面来说一下今天遇到的一个问题吧 大家都知道IE下面如果要获得剪贴板里面的信息的话,代码应该如下所示 window.cli ...

  5. node.js模拟qq漂流瓶

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) node.js模拟简易漂流瓶,页面有扔瓶子和捡瓶子的功能,一个瓶子只能被捡到一次,阅读完就置状态位, ...

  6. css配合js模拟的select下拉框

    css配合js模拟的select下拉框 <!doctype html> <html> <head> <meta charset="utf-8&quo ...

  7. js模拟触发事件

     html标签元素封装着实用的[事件],但在很多时候,需要[模拟触发事件],比如 [按钮单机事件]  可以实实在在点击按钮触发该事件,但体验而言,很多时候需要js逻辑处理让实现 触发事件的效果这时就用 ...

  8. js模拟Map对象,实现key---value

    js模拟Map对象,实现key---value 根据java中map的属性,实现key----value保存 function Map() { var struct = function (key, ...

  9. 单篇文章JS模拟分页

    废话部分 前两天做了一个前台分页插件,支持ajax读取数据绑定前台 和 url带页码参数跳转两种方式.于是稍加改动,做了一个单篇文章js模拟分页的代码,为什么说是模拟分页呢?因为在服务器响应HTML请 ...

随机推荐

  1. 2018-2-13-win10-uwp-从-Unity-创建

    title author date CreateTime categories win10 uwp 从 Unity 创建 lindexi 2018-2-13 17:23:3 +0800 2018-2- ...

  2. 三、SpringBoot项目探究

    1.pom文件 父项目 <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...

  3. 【leetcode】424. Longest Repeating Character Replacement

    题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...

  4. C/C++ C++ 11 std::bind()

    { #define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA ...

  5. python dir()详解

    Python dir() 函数 Python 内置函数 描述 dir() 函数不带参数时,返回当前范围内的变量.方法和定义的类型列表:带参数时,返回参数的属性.方法列表.如果参数包含方法__dir__ ...

  6. CSP-S2019退役记。。。

    模拟赛的时候题目就比较迷,感觉不像联赛难度的. 考完正式赛才觉得这TM算个P. Day1: 写密码的监考同学的蜜汁字体让我傻了. 0和O是一样的,9和q是一样的,1和l是一样的-- 又没有冷静下来发现 ...

  7. select 1 from ... sql语句中的1解读

    摘自:http://blog.csdn.net/zengcong2013/article/details/48224509 select  1 from ..., sql语句中的1代表什么意思?查出来 ...

  8. python--函数的返回值、函数参数的使用、名称空间与作用域、函数嵌套、函数对象

    今天学习内容有函数的返回值.函数参数的使用.名称空间与作用域.函数嵌套. 下来我们一一查看. 函数的返回值 看几个栗子: def func(x): y=func() print(y) def foo( ...

  9. 理解webpack中的process.env.NODE_ENV

    参考资料 一. process 要理解 process.env.NODE_ENV 就必须要了解 process,process 是 node 的全局变量,并且 process 有 env 这个属性,但 ...

  10. flutter 接入阿里云OSS

    之前因为使用正常文件上传,用户多时拥堵无法正常上传,因此接入阿里OSS 来解决这个问题.本来打算整原生那块,看了下比较麻烦,用flutter dio 直接请求oss 完成 1.上传用到了image_p ...