Javascript ember.js中打印数组的问题
当我尝试包含ember.js库(ember-1.0.0-rc.7.js)时,我看到了一些奇怪的东西 我刚刚打印出的Java代码是一个javascript数组:Javascript ember.js中打印数组的问题,javascript,ember.js,Javascript,Ember.js,当我尝试包含ember.js库(ember-1.0.0-rc.7.js)时,我看到了一些奇怪的东西 我刚刚打印出的Java代码是一个javascript数组: <script type="application/javascript"> var songs = [ 'a','b','c']; console.debug(songs.toString()); for(key in songs) { console.debug(songs[key]); } </scri
<script type="application/javascript">
var songs = [ 'a','b','c'];
console.debug(songs.toString());
for(key in songs)
{
console.debug(songs[key]);
}
</script>
ember.js库出现这种情况的原因是什么?我如何解决这个问题
感谢您的建议,
谢谢
Dembjer.js必须向本机Array.prototype添加函数。您可以使用以下方法检查每个键是否实际上是数组本身的属性,而不是继承的属性: 但是,通常建议始终使用普通for数组循环,因为它们具有数字键:
for(var i = 0; i < songs.length; i++) {
console.debug(songs[i]);
}
默认情况下,Ember扩展了内置原型,例如
Array.prototype
,为不支持的浏览器提供额外的方法或填充ES5方法。您看到这些方法是因为。这些包括所有属性,甚至是通过原型链继承的属性
相反,您应该使用常规的for
循环来迭代数组:
for(var i=0; i<songs.length; i++) {
console.debug(songs[i]);
}
或者,如果您不打算使用Ember的原型扩展,或者它们可能与其他库/脚本冲突,您可以通过配置来禁用Ember的原型扩展。余烬文档中专门介绍了这个问题。是的,谢谢阅读
for(var i = 0; i < songs.length; i++) {
console.debug(songs[i]);
}
songs.forEach(function(song){
console.debug(song);
});
for(var i=0; i<songs.length; i++) {
console.debug(songs[i]);
}
songs.forEach(function(song, i) {
console.debug(song);
});