我在这篇文章中读到,您可以使用 url.resolve 连接 URL:
url.resolve('http://example.com/', 'image.jpg') // 'http://example.com/image.jpg'
Run Code Online (Sandbox Code Playgroud)
但在阅读文档后,我发现这现在已被弃用。有 url.resolve 的替代品吗?
我想在Arrays上添加一个"插入"方法.所以我这样做:
> Array.prototype.insert = function(index, element){
this.splice(index, 0, element);
};
Run Code Online (Sandbox Code Playgroud)
它有效:
> a = [1,2,3]
[1, 2, 3]
> a.insert(0, 4)
undefined
> a
[4, 1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
但是有一种不良的副作用:
> for (i in a){console.log(i)}
0
1
2
3
insert
> for (i in a){console.log(a[i])}
4
1
2
3
function (index, element){
this.splice(index, 0, element);
}
Run Code Online (Sandbox Code Playgroud)
此行为不是打算,并打破我使用的其他库.这有什么优雅的解决方案吗?