javascript自定义范围绑定功能

编程入门 行业动态 更新时间:2024-10-13 10:24:39
本文介绍了javascript自定义范围绑定功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在阅读本文- www.robertsosinski/2009/04/28/binding-scope-in​​-javascript/ -创建自定义绑定函数的地方.

I am reading this article - www.robertsosinski/2009/04/28/binding-scope-in-javascript/ - where a custom bind function is made.

Function.prototype.bind = function(scope) { var _function = this; return function() { return _function.apply(scope, arguments); } } alice = { name: "alice" } eve = { talk: function(greeting) { console.log(greeting + ", my name is " + this.name); }.bind(alice) // <- bound to "alice" } eve.talk("hello"); // hello, my name is alice

我的问题是这条线特别是

My question is this line in particlar

return function() { return _function.apply(scope, arguments); }

为什么在_function.apply(作用域,参数)中返回;那里?它在做什么以及正在返回什么?我删除了该退货,但仍然有效.

Why is the return in _function.apply(scope, arguments); there? And what is it doing and what is being returned? I removed that return and it still works.

推荐答案

Why is the return in _function.apply(scope, arguments); there? And what is it doing and what is being returned? I removed that return and it still works.

这是您要返回值的地方.当前,您的talk函数没有返回任何值,因此您不需要它.如果您将通话功能更改为

This is there in case you want to return a value. Currently your talk function is not returning any value so you don't need it. if you change your talk function to

eve = { talk: function(greeting) { return ( greeting + ", my name is " + this.name) ; }.bind(alice) // <- bound to "alice" } console.log(eve.talk("hello"));

现在您将了解为什么需要退货

Now you will realize why return is required

更多推荐

javascript自定义范围绑定功能

本文发布于:2023-11-28 12:36:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   绑定   功能   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!