Why use the javascript function wrapper (added in coffeescript) quot;.call(this)quot;(为什么要使用javascript函数包装器(在coffeescript中添加)“.call(this)?)
问题描述
当我使用最新 (1.0) 版本的咖啡脚本时,一个简单的 javascript 输出看起来像这样(默认情况下):
When I use the latest (1.0) release of coffee-script, a simple javascript output looks like this (by default):
(function() {
var a;
a = 1;
}).call(this);
.call(this) 有什么作用以及添加它的原因是什么?
What does .call(this) do and what would be the reason to add it?
推荐答案
它正在创建一个函数,然后使用父函数/对象范围调用自身.
It's creating a function and then calling itself with the parent function/objects scope.
.call 和 .apply 是调用函数的不同方法.您基本上创建了一个函数,除了在自己的范围内设置 a=1 之外什么都不做.
.call and .apply are different methods of invoking a function. You basically created a function that does nothing except set a=1 within its own scope.
在 javascript 中你需要意识到每个函数都是一个对象,而 this 是指当前的对象/函数.使用 .call(this) 覆盖函数内的 this 并将其替换为调用上下文中的那个.
In javascript you need to realize that every function is a object, and this is what refers to the current object/function. Using .call(this) overrides this from within the function and replaces it with the one from the calling context.
这篇关于为什么要使用javascript函数包装器(在coffeescript中添加)“.call(this)"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么要使用javascript函数包装器(在coffeescript中添加)“.call(this)"?
基础教程推荐
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
