模拟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. 别再说你不会ElasticSearch调优了,都给你整理好了

    ES 发布时带有的默认值,可为 ES 的开箱即用带来很好的体验.全文搜索.高亮.聚合.索引文档 等功能无需用户修改即可使用,当你更清楚的知道你想如何使用 ES 后,你可以作很多的优化以提高你的用例的性 ...

  2. 爬虫示例--requests-module

    reuqests_test .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { ...

  3. noip2018火柴棒等式

    以下题目摘自洛谷p1149 给你n根火柴棍,你可以拼出多少个形如“A+B=CA+B=C”的等式?等式中的AA.BB.CC是用火柴棍拼出的整数(若该数非零,则最高位不能是00).用火柴棍拼数字0-90− ...

  4. python3.x 扯扯【切片】这玩意儿

    在此之前先了解一下list这个玩意儿: list对应cpp这的数组,一维数组,二维数组,或者是嵌套都行: L=[] #空列表 L=[1,2,3,4,5,6] #六项 L=['a',['b','c']] ...

  5. sparksql笔记

    1.sparksql是Spark用来处理结构化数据的一个模块,它提供了两个抽象DataFrame和DataSet并且作为分布式SQL查询引擎的作用. Hive SQL转换成MapReduce然后提交到 ...

  6. Halo(四)

    BeanWrapper 接口 操作属性 package org.springframework.beans; BeanWrapper bw = new BeanWrapperImpl(beanObje ...

  7. python 的按位与、或、异或 运算

    符号 描述 运算规则                        by MoreWindows & 与 两个位都为1时,结果才为1     (统计奇数) | 或 两个位都为0时,结果才为0  ...

  8. LOJ 2552 「CTSC2018」假面——DP

    题目:https://loj.ac/problem/2552 70 分就是 f[i][j] 表示第 i 个人血量为 j 的概率.这部分是 O( n*Q ) 的:g[i][j][0/1] 表示询问的人中 ...

  9. python复制文件到文件夹中

    目标:将一张图片复制到一个文件夹下 所有子文件中. import shutil import os #第一部分,准备工作,拼接出要存放的文件夹的路径 file = 'E:/测试/1.jpg' #cur ...

  10. symantec SMG 抓包

    1.使用putty连接SMG,先用admin账号登陆 执行set-support  , 设置一个密码. 2.重新打开一个putty连接SMG,使用support账号登陆.用我们刚才设置的密码. 3.开 ...