技术分享
首页
  • JavaScript

    • 构造函数和原型
    • Cookie和Session
    • Object.create(null)和{}
    • TypeScript配置
    • typescript入门到进阶
  • 框架

    • Vue-Router
    • React基础入门
  • 其它

    • Http协议
    • 跨域问题总结
  • 分析Promise实现
  • Axios源码分析
  • Webpack原理
  • vueRouter源码分析
  • Vue

    • Vite快速搭建Vue3+TypeScript项目
    • Cordova打包Vue项目的问题
    • Vue将汉字转为拼音,取出首字母
  • JavaScript

    • new Function
  • 后端

    • Node.js中使用Crypto生成Token
    • Body-Parser处理多层对象的问题
  • 其它

    • 项目Demo汇总
    • Vuepress+Vercel搭建个人站点
    • 项目中能用到的
    • husky规范代码提交
  • Mongoose基础
  • Multer文件上传中间件的使用
  • JavaScript

    • 浅谈两数全等
    • JavaScript进制转换
    • 手写bind,apply,call和new
  • 算法

    • 数组去重和排序
    • 数组扁平化
    • 斐波那契数列
  • JavaScript 数据结构
  • 其它

    • webpack面试题
    • vite面试题
    • svg和canvas的优缺点
    • TypeScript面试题
    • Vue常见面试题
  • 计算机网络

    • 数据链路层
    • 网络层
  • Git的使用
  • Nginx的使用
  • CentOS7安装Nginx
  • 正则表达式
  • SEO搜索引擎优化
  • Serverless介绍
友链
GitHub (opens new window)

鑫生活

总有人要赢,为什么不能是我
首页
  • JavaScript

    • 构造函数和原型
    • Cookie和Session
    • Object.create(null)和{}
    • TypeScript配置
    • typescript入门到进阶
  • 框架

    • Vue-Router
    • React基础入门
  • 其它

    • Http协议
    • 跨域问题总结
  • 分析Promise实现
  • Axios源码分析
  • Webpack原理
  • vueRouter源码分析
  • Vue

    • Vite快速搭建Vue3+TypeScript项目
    • Cordova打包Vue项目的问题
    • Vue将汉字转为拼音,取出首字母
  • JavaScript

    • new Function
  • 后端

    • Node.js中使用Crypto生成Token
    • Body-Parser处理多层对象的问题
  • 其它

    • 项目Demo汇总
    • Vuepress+Vercel搭建个人站点
    • 项目中能用到的
    • husky规范代码提交
  • Mongoose基础
  • Multer文件上传中间件的使用
  • JavaScript

    • 浅谈两数全等
    • JavaScript进制转换
    • 手写bind,apply,call和new
  • 算法

    • 数组去重和排序
    • 数组扁平化
    • 斐波那契数列
  • JavaScript 数据结构
  • 其它

    • webpack面试题
    • vite面试题
    • svg和canvas的优缺点
    • TypeScript面试题
    • Vue常见面试题
  • 计算机网络

    • 数据链路层
    • 网络层
  • Git的使用
  • Nginx的使用
  • CentOS7安装Nginx
  • 正则表达式
  • SEO搜索引擎优化
  • Serverless介绍
友链
GitHub (opens new window)
  • 浅谈两数全等
  • JavaScript 进制转换
  • 数组去重和排序
  • 数组扁平化
  • 斐波那契数列
  • webpack面试题
  • 手写bind,apply,call和new
    • 实现 bind
    • 实现 apply
    • 实现 call
    • 实现 new
  • vite面试题
  • svg和canvas的优缺点
  • TypeScript面试题
  • Vue常见面试题
  • JavaScript 数据结构
  • 面试
coderly
2021-03-18

手写bind,apply,call和new

# 手写 bind,apply,call 和 new

# 实现 bind

Function.prototype.MyBind = function() {
  var _this = this
  var context = arguments.shift()
  var args = Array.prototype.slice.call(arguments)
  return function Fn() {
    var _args = [].concat(args, Array.prototype.slice.call(arguments))
    return _this.apply(context, _args)
  }
}
1
2
3
4
5
6
7
8
9

# 实现 apply

Function.prototype.MyApply = function() {
  var fn = arguments.shift()
  var args = Array.prototype.slice.call(arguments[0])
  fn.__this = this

  var result = fn.__this(...args)
  delete fn.__this
  return result
}
1
2
3
4
5
6
7
8
9

# 实现 call

Function.prototype.MyCall = function() {
  var fn = arguments.shift()
  var args = Array.prototype.slice.call(arguments)
  fn.__this = this

  var result = fn.__this(...args)
  delete fn.__this
  return result
}
1
2
3
4
5
6
7
8
9

# 实现 new

function MyNew (Fn, ...args) {
  var obj = Object.create(Fn.prototype)
  let result = Fn.apply(obj, args)
  return typeof result === 'object' ? result : obj
}
1
2
3
4
5
#JavaScript
上次更新: 2021/09/13, 15:11:59
webpack面试题
vite面试题

← webpack面试题 vite面试题→

最近更新
01
css
09-13
02
html
09-13
03
README
09-13
更多文章>
Theme by Vdoing | Copyright © 2021-2021 coderly | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式