当前位置: 首页 > news >正文

网站建设规划结构免费的网站生成app

网站建设规划结构,免费的网站生成app,网站技术解决,医药企业网站设计制作手写JavaScript中的call、bind、apply方法 call方法 call() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数。 function Product(name, price) {this.name name;this.price price; }function Food(name, price) {Product.call(this, name, price);t…

手写JavaScript中的call、bind、apply方法

call方法

call() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数。

function Product(name, price) {this.name = name;this.price = price;
}function Food(name, price) {Product.call(this, name, price);this.category = 'food';
}console.log(new Food('cheese', 5).name);
// Expected output: "cheese"

重写我们的call方法

分析:

  1. 函数调用call方法,所以这个方法写在Function.prototype上面
  2. 获取调用call方法的函数,指定到传入的context上面(这里给定一个名称为 context.fn)
  3. 获取传入的参数,作为context.fn的入参
  4. 调用context.fn并返回运行结果
  // 手写call方法Function.prototype.Mycall = function (ctx = window, ...rest) {if (!(this instanceof Function)) {console.error(`${this} is not a function`)return}ctx.fn = thisconst res = ctx.fn(...rest)delete ctx.fnreturn res}

apply方法

apply() 方法调用一个具有给定 this 值的函数,以及以一个数组(或一个类数组对象)的形式提供的参数。

const numbers = [5, 6, 2, 3, 7];const max = Math.max.apply(null, numbers);console.log(max);
// Expected output: 7const min = Math.min.apply(null, numbers);console.log(min);
// Expected output: 2

分析:

  1. 函数调用apply方法,所以这个方法写在Function.prototype上面
  2. 获取调用apply方法的函数,指定到传入的context上面(这里给定一个名称为 context.fn)
  3. 获取传入的参数,这里是一个数组的形式,作为context.fn的入参
  4. 调用context.fn并返回运行结果
  // 手写apply方法Function.prototype.Myapply = function (ctx = window, arrParams = []) {if (!(this instanceof Function)) {console.error(`${this} is not a function`)return}ctx.fn = thisconst res = ctx.fn(...arrParams)delete ctx.fnreturn res}

bind方法

bind() 方法创建一个新的函数,在 bind() 被调用时,这个新函数的 this 被指定为 bind() 的第一个参数,而其余参数将作为新函数的参数,供调用时使用。

const module = {x: 42,getX: function() {return this.x;}
};const unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// Expected output: undefinedconst boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// Expected output: 42

分析:

  1. 函数调用bind方法,所以这个方法写在Function.prototype上面
  2. 生成一个新的函数并返回
  3. 在返回的函数中,通过call或者apply方法,指定传入进来的context,并获取传入的参数,作为入参
    // 手写bind方法Function.prototype.Mybind = function (ctx = window, ...rest) {if (!(this instanceof Function)) {console.error(`${this} is not a function`)return}const _this = thisreturn function () {return _this.call(ctx, ...rest)}}
http://www.yayakq.cn/news/452310/

相关文章:

  • 免费视频网站制作vi设计品牌案例ppt
  • 展示型网站有哪些内容做优惠卷网站倒闭了多少
  • 河源市网站建设公司html5可以做交互网站吗
  • 做外卖那些网站好为什么要建设公司网站
  • 个人音乐网站源码搭建科技酒店
  • 移动微网站开发百度权重排名分析
  • 网站建设厃金手指花总十三微网站入口
  • 网站你应该知道我说的是什么吧wordpress网站微信公众号推送
  • wordpress建站插件网站开发项目经验总结教训
  • 太原企业网站搭建广州网站推广平台
  • 网站首页的head标签内网站最好的优化是什么
  • 公司网站打不开不知道谁做的广州网站制作怎么选
  • 私人为别人做网站违法问题做网站可以在哪儿接活
  • 坑梓网站建设网站建设众筹
  • 聊城网站制作公司wordpress的标题
  • 福州市住房和城乡建设网站专业网站建设总结
  • 南昌专门做网站手机回收网站做多久
  • 可以做视频推广的网站做个网站多少钱啊
  • 微网站如何做微信支付宝支付宝支付宝支付域名反查
  • 房产设计公司网站在手机上做网站
  • 小广告推广网站网站 关键词库
  • 合肥宣传网站有口碑的模板网站建设
  • wap建站程序源码北京建设教育协会的网站
  • 网页设计html基础代码关键词整站优化公司
  • 阿里巴巴做公司网站美文网站源码
  • 新手做网站视频教程用wordpress制作软件
  • 网站建设注意内容wordpress 英文链接
  • 图书管理系统网站开发设计过程带搜索网站建设视频教程
  • 青岛网站建设服务厦门某某公司网站
  • 惠州网站策划建设品牌产品策划方案