JS模拟实现题目(new debounce throwee 等)
模拟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 等)的更多相关文章
- js模拟抛出球运动
js练手之模拟水平抛球运动 -匀加速运动 -匀减速运动 模拟运动有些基本的思路,当前所在点的坐标,元素的长宽是多少,向右/向下运动x/y增加,向上/向左运动x/y减少,运动的路程是多少,用什么方程进行 ...
- Gremlins.js – 模拟用户随机操作的 JS 测试库
Gremlins.js 是基于 JavaScript 编写的 Monkey 测试库,支持 Node.js 平台和浏览器中使用.Gremlins.js 随机模拟用户操作:单击窗口中的任意位置,在表格中输 ...
- JS 模拟手机页面文件的下拉刷新
js 模拟手机页面文件的下拉刷新初探 老总说需要这个功能,好吧那就看看相关的东西呗 最后弄出了一个简单的下拉刷新页面的形式,还不算太复杂 查看 demo 要在仿真器下才能看到效果,比如chrome的里 ...
- 由chrome剪贴板问题研究到了js模拟鼠标键盘事件
写在前面 最近公司在搞浏览器兼容的事情,所有浏览器兼容的问题不得不一个人包了.下面来说一下今天遇到的一个问题吧 大家都知道IE下面如果要获得剪贴板里面的信息的话,代码应该如下所示 window.cli ...
- node.js模拟qq漂流瓶
(文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) node.js模拟简易漂流瓶,页面有扔瓶子和捡瓶子的功能,一个瓶子只能被捡到一次,阅读完就置状态位, ...
- css配合js模拟的select下拉框
css配合js模拟的select下拉框 <!doctype html> <html> <head> <meta charset="utf-8&quo ...
- js模拟触发事件
html标签元素封装着实用的[事件],但在很多时候,需要[模拟触发事件],比如 [按钮单机事件] 可以实实在在点击按钮触发该事件,但体验而言,很多时候需要js逻辑处理让实现 触发事件的效果这时就用 ...
- js模拟Map对象,实现key---value
js模拟Map对象,实现key---value 根据java中map的属性,实现key----value保存 function Map() { var struct = function (key, ...
- 单篇文章JS模拟分页
废话部分 前两天做了一个前台分页插件,支持ajax读取数据绑定前台 和 url带页码参数跳转两种方式.于是稍加改动,做了一个单篇文章js模拟分页的代码,为什么说是模拟分页呢?因为在服务器响应HTML请 ...
随机推荐
- LLppdd likes strings
LLppdd's likes strings! Time Limit: 1 s Memory Limit: 256 MB 题目背景 LLppdd 由于实在是太弱了,在 \(ION 2018\) 模拟十 ...
- 2018-8-10-wpf-绑定-DataGridTextColumn-
title author date CreateTime categories wpf 绑定 DataGridTextColumn lindexi 2018-08-10 19:16:53 +0800 ...
- Pandas之loc\iloc\ix
---------------------------------------------------------------------------------------------------- ...
- log4j.xml配置详解(转)
转自:http://willow-na.iteye.com/blog/347340 Xml代码 <?xml version="1.0" encoding="UTF- ...
- Sass-属性嵌套
Sass 中还提供属性嵌套,CSS 有一些属性前缀相同,只是后缀不一样,比如:border-top/border-right,与这个类似的还有 margin.padding.font 等属性.假设你的 ...
- 微信小程序 textarea的placeholder层级过高 在弹层之上 bug解决方法
微信小程序textarea的placeholder的层级一直都是一个神坑, 我们是没有办法将我们的弹层加大层级去盖过placeholder的, 所以要解决这个问题只能从另外的角度找思路 我的思路是 : ...
- shell中的空格【转载】
1.定义变量时, =号的两边不可以留空格. eg: gender=femal————right gender =femal———–wrong gender= femal———–wrong gender ...
- Java 基础 - 自动装箱,valueOf装箱,new -->使用 == 和 equals比较
总结 关于equals 比较 记住:equals方法比较的是真正的值 两个包装类比较,比较的是包装的基本数据类型的值 基本数据类型和包装类型比较时,会先把基本数据类型包装后再比较 (但是因为equa ...
- Linux进程基本原理
主题进程介绍 一进程相关概念 内核的功用:进程管理.文件系统.网络功能.内存管理.驱动程序.安全功能等 在操作系统上会运行多个应用程序,应用程序分配多大的内存都由内核实现 程序文件 程序和进程的关系 ...
- Python--线程队列(queue)、multiprocessing模块(进程对列Queue、管道(pipe)、进程池)、协程
队列(queue) 队列只在多线程里有意义,是一种线程安全的数据结构. get与put方法 ''' 创建一个“队列”对象 import queue q = queue.Queue(maxsize = ...