js遍历数组和数组对象

<script>
    //----------------for用来遍历数组对象--
    var i,myArr = [1,2,3];
    for (var i = 0; i < myArr.length; i++) {
        console.log(i+":"+myArr[i]);
    };
    ---------for-in 用来遍历非数组对象
    var man ={hands:2,legs:2,heads:1};
    为所有的对象添加clone方法,即给内置原型(object,Array,function)增加原型属性,该方法很强大,也很危险
    if(typeof Object.prototype.clone ==="undefined"){
        Object.prototype.clone = function(){};    
    }
    //
    for(var i in man){
        if (man.hasOwnProperty(i)) { filter,只输出man的私有属性
            console.log(i,":",man[i]);
        };
    }
    输出结果为print hands:2,heads:1

    in man) {不使用过滤
        console.log(i,man[i]);
    }    
    输出结果为
    hands : 2 index.html:20
    legs : 2 index.html:20
    heads : 1 index.html:20
    clone : function (){} 
     man) {
        if(Object.prototype.hasOwnProperty.call(man,i)) { 过滤
            console.log(i,man[i]);
        }
    }
   

相关文章

kindeditor4.x代码高亮功能默认使用的是prettify插件,prett...
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小