面试题:实现call、apply、bind
面试题:实现call、apply、bind
实现bind
module.exports = function(Tcontext, ...args) {
let globalThis = typeof window === "undefined" ? global : window;
let context = typeof Tcontext === "undefined" ? globalThis : Tcontext;
let withArgs = args.length != 0; //是否传参
if (!!this.prototype && typeof context === "object" && context !== null) {
//context是有效对象
return withArgs
? () => Object.assign(context, new this(...args)) //若带参,返回的函数不需要处理接收参数
: (...args) => Object.assign(context, new this(...args)); //若不带参,返回的函数需要处理接收参数
} else {
return withArgs
? ()=>this(...args)
: (...args)=>this(...args)
}
};
实现call
在实现了bind后,只需要将call转换给bind处理就可以了
module.exports = function(context, ...args){
return this.newBind(context, ...args)()
}
实现apply
实现了call后,只需要处理下参数,转换给call处理就可以了
module.exports = function(context, args){
return args instanceof Array?this.newCall(context, ...args):this.newCall(context)
}
使用
const newBind = require("./bind")
const newCall = require("./call")
const newApply = require("./apply")
Function.prototype.newBind = newBind //将bind挂载到Function原型,使得任何实例可以像使用bind一般使用newBind
Function.prototype.newCall = newCall //将call挂载到Function原型,使得任何实例可以像使用call一般使用newCall
Function.prototype.newApply = newApply //将apply挂载到Function原型,使得任何实例可以像使用apply一般使用newApply
在挂载到原型上后,就可以正常使用了
测试
1、bind的测试
require(".") //导入模块
const obj = {
q: "1"
}
const Parent = function(a, b){
this.a = a;
this.b = b
}
//一、使用bind的函数是构造函数,context是有效对象
//测试bind带参,执行函数不带参的情况
try {
console.log(Parent.newBind(obj,3,2)())
} catch (error) {
if(error.toString()=="Illegal context") console.log("err")
else console.log("Failed")
}
//测试bind带参,执行函数带参的情况 => 执行函数的参数不生效
try {
console.log(Parent.newBind(obj,3,2)(3,4))
} catch (error) {
if(error.toString()=="Illegal context") console.log("err")
else console.log("Failed")
}
//测试bind不带参的情况,执行函数不带参的情况 => 执行函数的参数应为undefined
try {
console.log(Parent.newBind(obj)())
} catch (error) {
if(error.toString()=="Illegal context") console.log("err")
else console.log("Failed")
}
//测试bind带参,执行函数带参的情况 => 执行函数的带参应生效
try {
console.log(Parent.newBind(obj)(3,4))
} catch (error) {
if(error.toString()=="Illegal context") console.log("err")
else console.log("Failed")
}
//二、使用bind的函数是构造函数,context是无效对象
//测试bind带参,执行函数不带参的情况
console.log(Parent.newBind(null,3,2)())
//测试bind带参,执行函数带参的情况
console.log(Parent.newBind(null,3,2)(3,4))
//测试bind不带参的情况,执行函数不带参的情况
console.log(Parent.newBind(null)())
//测试bind带参,执行函数带参的情况
console.log(Parent.newBind(null)(3,4))
//三、使用bind的函数不是构造函数
console.log(Math.pow.newBind(obj, 3, 2)())
console.log(Math.pow.newBind(null, 3, 2)())
console.log(Math.pow.newBind(1, 3, 2)())
console.log(Math.pow.newBind(null)(2,3))
console.log("测试完成")
2、call的测试
require(".") //导入模块
const obj = {
q: "1"
}
const Parent = function(a, b){
this.a = a;
this.b = b
}
//一、使用call的函数是构造函数,context是有效对象
//测试call带参
try {
console.log(Parent.newCall(obj,3,2))
} catch (error) {
if(error.toString()=="Illegal context") console.log("err")
else console.log("Failed")
}
//测试call不带参的情况 => 执行函数的参数应为undefined
try {
console.log(Parent.newCall(obj))
} catch (error) {
if(error.toString()=="Illegal context") console.log("err")
else console.log("Failed")
}
//二、使用call的函数是构造函数,context是无效对象
//测试call带参
console.log(Parent.newCall(null,3,2))
//测试call不带参的情况
console.log(Parent.newCall(null))
//三、使用call的函数不是构造函数
console.log(Math.pow.newCall(obj, 3, 2))
console.log(Math.pow.newCall(null, 3, 2))
console.log(Math.pow.newCall(1, 3, 2))
console.log("测试完成")
3、apply的测试
require(".") //导入模块
const obj = {
q: "1"
}
const Parent = function(a, b){
this.a = a;
this.b = b
}
//一、使用apply的函数是构造函数,context是有效对象
//测试apply带参
try {
console.log(Parent.newApply(obj,[3,2]))
} catch (error) {
if(error.toString()=="Illegal context") console.log("err")
else console.log("Failed")
}
//测试apply不带参的情况 => 执行函数的参数应为undefined
try {
console.log(Parent.newApply(obj))
} catch (error) {
console.log(error);
if(error.toString()=="Illegal context") console.log("err")
else console.log("Failed")
}
//二、使用apply的函数是构造函数,context是无效对象
//测试apply带参
console.log(Parent.newApply(null,[3,2]))
//测试apply不带参的情况
console.log(Parent.newApply(null))
//三、使用apply的函数不是构造函数
console.log(Math.pow.newApply(obj, [3, 2]))
console.log(Math.pow.newApply(null, [3, 2]))
console.log(Math.pow.newApply(1, [3, 2]))
console.log(Math.pow.newApply(1, 3, 2)) //第二个参数不为数组,结果应异常
console.log("测试完成")
经过测试,实现call、apply、bind基本功能是ok的
github地址
面试题:实现call、apply、bind的更多相关文章
- 前端JS面试题汇总 Part 3 (宿主对象与原生对象/函数调用方式/call与apply/bind/document.write)
原文:https://github.com/yangshun/front-end-interview-handbook/blob/master/questions/javascript-questio ...
- 前端面试 js 你有多了解call,apply,bind?
函数原型链中的 apply,call 和 bind 方法是 JavaScript 中相当重要的概念,与 this 关键字密切相关,相当一部分人对它们的理解还是比较浅显,所谓js基础扎实,绕不开这些基础 ...
- JavaScript内置一些方法的实现原理--new关键字,call/apply/bind方法--前戏
new关键字,call/apply/bind方法都和this的绑定有关,在学习之前,首先要理解this. 一起来学习一下this吧 首先.this是一个对象. 对象很好理解,引用类型值,可以实现如th ...
- this指向与call,apply,bind
this指向与call,apply,bind ❝ 「this」问题对于每个前端同学来说相信都不陌生,在平时开发中也经常能碰到,有时候因为「this」还踩过不少坑,并且「this」问题在面试题中出现的概 ...
- call,apply,bind的用法
关于call,apply,bind这三个函数的用法,是学习javascript这门语言无法越过的知识点.下边我就来好好总结一下它们三者各自的用法,及常见的应用场景. 首先看call这个函数,可以理解成 ...
- JavaScript中call,apply,bind方法的总结。
why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user:"追梦子", fn:f ...
- call(),apply(),bind()与回调
1.call(),apply(),bind()方法 JavaScript 中通过call或者apply用来代替另一个对象调用一个方法,将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定 ...
- JS 的 call apply bind 方法
js的call apply bind 方法都很常见,目的都是为了改变某个方法的执行环境(context) call call([thisObj[,arg1[, arg2[, [,.argN]]]] ...
- javascript-this,call,apply,bind简述2
上节我们一起研究了this这个小兄弟,得出一个结论,this指向调用this所在函数(或作用域)的那个对象或作用域.不太理解的朋友可以看看上节的内容,这次我们主要探讨一下call(),apply(), ...
- javascript-this,call,apply,bind简述1
最近在系统的学习面向对象方面的知识,遇到的最大拦路虎就数this的指向,call,apply,bind函数的使用,单独抽出一天时间把这几个烦人的家伙搞定,去学习更深入的内容. 首先介绍一下this的一 ...
随机推荐
- 字符串函数-unquote()函数
字符串函数顾名思意是用来处理字符串的函数.Sass 的字符串函数主要包括两个函数: unquote($string):删除字符串中的引号: quote($string):给字符串添加引号. 1.unq ...
- 人生苦短_我用Python_pymysql库对Mysql数据库操作_009
# coding=utf-8 import pymysql ''' 数据库的登录信息: config={ 'host':'118.126.108.xxx', # :主机 'user':'python' ...
- Python格式输出汇总
print ('%10s'%('test')) print ('{:<10}'.format('test'))#left-aligned print ('{:>10}'.format('t ...
- Bootstrap的本地引入
今天用前端框架时选择了Bootstrap,然后东西都下好了本地就是引入不进去. 查了一下发现必须jquery要在BootStrap之前引入,然后我更改了引入顺序,发现还是不行 <script s ...
- SQL笔试
一.题目 二.练习数据 SELECT VERSION(); create table CDM_AGENT(ano VARCHAR(6),aname VARCHAR(8)); create table ...
- 偏函数(partial)
from functools import partial def add(a,b,c,d): return a+b+c+d add = partial(add,1,2) print(add(3,4) ...
- 线程join方法demo-模拟叫号看病
package cn.chapter4.test5; public class SicknessDemo { /** * 模拟叫号看病 * @param args * * 思路:把普通号看病写在主线程 ...
- SCJP读书之知识点:
1:实例变量和局部变量 实例变量:是在类中进行声明的,可以有public,private等修饰符进行修饰. 局部变量:在方法中进行声明,生命周期就是方法开始,到方法结束.但是可以进行对象的引用来调用. ...
- php htmlentities()函数 语法
php htmlentities()函数 语法 作用:把字符转换为 HTML 实体 语法:htmlentities(string,flags,character-set,double_encode) ...
- BZOJ 3456: 城市规划(dp+多项式求逆)
传送门 解题思路 这道题就是求带标号的无向连通图个数,首先考虑\(O(n^2)\)的做法,设\(f_i\)表示有\(i\)个节点的无向连通图个数,那么考虑容斥,先把所有的无向图求出,即为\(2^{C( ...