直接在函数声明中使用function.prototype.bind

编程入门 行业动态 更新时间:2024-10-14 14:17:03
本文介绍了直接在函数声明中使用function.prototype.bind的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为什么允许这样做?

var f = function() { console.log(this.x); }.bind({x:1})();

为什么这不是或更好为什么我在这种情况下会出现语法错误?

And why this is not or better why I get syntax error in this case ?

function f() { console.log(this.x); }.bind({x:1})();

那么,为什么我需要函数表达式语法来完成这项工作,有没有办法直接在函数声明上使用 bind 方法?

So, why I need function expression syntax to get this work and is there a way to use bind method directly on function declaration ?

推荐答案

第二个例子有效,但语法稍微偏离:

The second example works but the syntax is slightly off:

将函数包含在parens中。我不得不说,我不完全确定为什么。似乎没有parens会工作吗? :P

Surround the function in parens. I have to say that I'm not entirely sure why. It seems like it would work without the parens huh? :P

(function f() { console.log(this.x); }).bind({x:1})();

更多推荐

直接在函数声明中使用function.prototype.bind

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

发布评论

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

>www.elefans.com

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