作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
function Foo(){
}
Foo.prototype={
method1:function(o){
console.log(o);
}
,shorthand:function(){
if(!arguments.length || typeof arguments[0]=='undefined') {
arguments[0]={};
}
arguments[0].bar='test';
return this.method1.apply(this,arguments);
}
}
var instance=new Foo();
instance.shorthand();
console.log(o); // returns undefined
一段时间后我发现,为数组分配参数可以解决这个问题。
arguments=[{bar:'test'}];
我发现,arguments 毕竟不是数组,也不是半数组。好吧,它没有推送方法。
为什么这样做(我的意思是返回未定义)?它是出于某种目的而制作的吗?
最佳答案
您已经看到 arguments
不是数组,而只是“类数组”。它是一个具有数字键和 length
属性的普通对象:
你的代码
arguments[0]={};
arguments[0].bar='test';
确实以某种方式起作用。
但因为它不是数组,所以length
属性没有更新。所以尽管你添加了一个属性,length
仍然是 0
:
现在,我不知道 apply
的实现,但我假设它使用 length
属性迭代“数组”中的所有元素。如果长度为零,它不会向函数传递任何参数,因此 o
在这种情况下确实是 undefined
。
如果你添加
arguments.length = 1;
将数组分配给 arguments
只会覆盖对象,将元素分配给数组会更新已知的 length
。
关于Javascript:参数随着进一步的应用处理而改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8399162/
我有这个: const {ops} = getOplogStreamInterpreter(strm); ops.del.subscribe(v => { console.log('delete
我四处搜索,据我所知,POST 表单请求已被限制为 10MB (http://golang.org/src/net/http/request.go#L721)。 如果我要在我的 ServeHTTP 方
我是一名优秀的程序员,十分优秀!