gpt4 book ai didi

javascript - 使用 forEach 枚举属性和值

转载 作者:行者123 更新时间:2023-11-28 15:15:38 25 4
gpt4 key购买 nike

下面的代码没有枚举网页上 Array 构造函数的属性和值。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
window.onload = myfunc;
function myfunc(){
window['Object']['getOwnPropertyNames'] (window['Array'])['forEach'](function(val){
document.write(val + ": " + Array[val] + "<br />" );
});
}
}
</script>
</head>
<body>

</body>
</html>

传递给 forEach 的参数是否有任何问题?

最佳答案

不,参数没问题,你有一个额外的 } 右括号。​​

window.onload = myfunc;
function myfunc(){
var x = "";
window['Object']['getOwnPropertyNames'] (window['Array'])['forEach'](function(val){
x += val + ": " + Array[val] + "<br />" ;
});
document.write(x);
}

我希望您知道 document.write 将替换整个文档。此外,我已将您想要打印的任何内容附加到变量 x 中,您可以使用此 x 来显示输出。

关于javascript - 使用 forEach 枚举属性和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34103288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com