- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在学习 JavaScript,对 arguments
属性数组感到很困惑。
我有一个接受单个参数并返回它的函数。当我传递参数并使用 arguments[0] = value
重新分配它时,它正在更新值。
function a(b) {
arguments[0] = 2;
return b;
}
console.log(a(1)); //returns 2
但是当我不带参数调用同一个函数时,它返回 undefined
。
function a(b) {
arguments[0] = 2;
return b;
}
console.log(a()); //returns undefined
但即使我传递 undefined
,该值也会更新。
function a(b) {
arguments[0] = 2;
return b;
}
console.log(a(undefined)); //returns 2
我认为如果你不向 JavaScript 函数传递参数,它会自动创建它并将值赋给 undefined
并且在更新后它应该反射(reflect)更新后的值,对吧?
还有 a()
和 a(undefined)
是一回事吧?
最佳答案
分配给 arguments
索引只会更改关联的参数值(我们称它为第 n
个参数)如果函数被调用至少 n
参数。 arguments
对象的数字索引属性本质上是 setter(和 getter):
下面的斜体字是我对这个过程如何与问题相关的评论:
(Let)
args
(be) the actual arguments passed to the [[Call]] internal method
Let
len
be the number of elements in args.Let
indx
=len - 1
.Repeat while
indx >= 0
, (so, the below loop will not run when no arguments are passed to the function:)(assign to the arguments object being created, here called
map
:)
- Add
name
as an element of the listmappedNames
.
- Let
g
be the result of calling theMakeArgGetter
abstract operation with argumentsname
andenv
.
- Let
p
be the result of calling theMakeArgSetter
abstract operation with argumentsname
andenv
.
- Call the [[DefineOwnProperty]] internal method of
map
passing ToString(indx
), the Property Descriptor {[[Set]]:p
, [[Get]]:g
, [[Configurable]]:true
}, andfalse
as arguments.
因此,如果不带参数调用该函数,arguments[0]
上将不会有 setter,因此重新分配它不会更改索引 0 处的参数。
同样的事情也会发生在其他指标上——如果你用 1 个参数调用一个函数,但是这个函数接受两个参数,分配给 arguments[1]
不会改变第二个参数,因为 arguments[1]
没有 setter:
function fn(a, b) {
arguments[1] = 'bar';
console.log(b);
}
fn('foo');
所以
a()
anda(undefined)
are the same thing right?
不是这种情况,因为第二个结果是一个 arguments
对象,在索引 0 上有一个 setter 和一个 getter,而第一个不是。
请注意,arguments
和函数参数之间的这种奇怪交互仅出现在草率模式中。在严格模式下,对 arguments
的更改不会对单个参数标识符包含的值产生任何影响:
'use strict';
function a(b) {
arguments[0] = 2;
return b;
}
console.log(a(1)); //returns 1
关于javascript - 使用参数数组更改 JavaScript 函数的参数值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54323086/
SELECT ID, AppID, Description, Min([Transaction Date]) AS TransactionDate FROM AppProsHist WHERE [De
目前我正在创建规则,该规则应该检查方法是否包含 @Test 和 @TestInfo 注释。如果确实如此,@TestInfo 不应有空参数 testCaseId。 有几种不同的可能方法来填充 testC
是否可以设置参数值,使其在 where 子句中始终结果为 true? 作为示例,考虑一个查询: SELECT name FROM student WHERE class=@parameter; 现在我
我是 JPA 的新手,这是我的查询之一,我有几个参数作为查询的一部分,任何参数都可以为空值 @Query(value = "SELECT ord.purchaseOrderNumber,ord.sal
LOD 参数对 texturelod 取什么值? ?我发现的规范根本没有提到它。它是百分比还是带有百分比的索引值。如果是后者,有没有办法获得纹理具有的 mipmap 数量,以便我能够使用百分比? 最佳
我希望此代码替换现有的 URL 参数“aspid”,但它的作用是在现有的 id 上添加另一个 id。有人可以帮忙吗? $(document).ready(function() { function
在 Spring-boot 项目中,我尝试将 Date 对象作为请求参数传递,并收到此错误: Parameter value [1] did not match expected type [java
在我们的 Jenkinsfile 中,我们有很多参数(参数化构建),在这种情况下,我想检查每个参数是否已切换并对其进行操作。这些参数具有相似的名称,但以不同的小数结尾,因此我想迭代它们以实现此目的。
我的模板之一中有类似于以下内容的内容: 但是 Freemarker 不高兴并给了我: Exception in thread "main" freemarker.core.ParseExceptio
我正在从表单向重定向 servlet 发送一个 post 请求。然后,Servlet 将表单写入其响应 (getWriter) 对象。该表单包含许多隐藏字段。我使用 javascript 提交此表单(
我正在创建一个 JavaScript 组件,我正在根据 jQuery 结果创建该组件的实例,但是,我传递到构造函数中的 DOM 元素虽然在我单步执行调用代码中的循环时已填充,但在传递给构造函数时是未定
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我对 javascript 有疑问。 假设我有这样的 javascript 函数: function show_popup(id) { alert(id); } 编
我目前正在尝试抓取嵌入式 m3u8 url 路径以进行自学。 到目前为止,我设法确定请求会生成带有 m3u8 信息的 json 响应。 例如,https://headlines.yahoo.co.jp
谷歌地图 API 需要这样的参数: NSString *urlString=@"http://maps.google.com/maps?saddr=43.2923,5.45427&daddr=43.4
“parameterValue”是在 Javascript 中的事件上传递的默认参数吗?谁能解释一下这个值从何而来。 Load Ajax content 我发现它在以下文章中使用 - http://w
我有一个 .SWF 电子邮件提交表单。背景颜色通过以下方式设置: `` 并嵌入: `` 是否可以将鼠标悬停在对象或包含的 div 上来更改这些值?即#ffffff 非常感谢! 最佳答案 将 wmode
假设我想用指数函数拟合两个数组x_data_one和y_data_one。为此,我可以使用以下代码(其中 x_data_one 和 y_data_one 被赋予虚拟定义): import numpy
有什么方法可以填充 parameters在基于外部属性文件内容的 Liquibase 变更日志文件中? 例如,我希望能够说: 并将 table.name 的值和
我必须按原样发送参数值 'AbCd/EfgH'。但是 Angular 将 '/' 转义为 %2F。我无法控制 URL。 解决这个问题的最佳方法是什么? 我不想强制 Angular 停止对所有其他 UR
我是一名优秀的程序员,十分优秀!