js对象属性枚举

2019-06-11  本文已影响0人  王牡丹爱写作

代码

        var a={name:"wangmudan",age:12}
        for (var pro in a) {
            console.log(a[pro]);//此时必须加上[]
        }
function A(){
            this.name="wangmazi";
            this.age=12;
                  this.getName=function(){
                console.log("我是打酱油的");
            }
        }
        A.prototype.sex="nan";
        var a=new A();
        for(pro in a){
            console.log(a[pro]);
                //wangmazi 12 ƒ (){console.log("我是打酱油的");} nan
        }
function A(){
            this.name="wangmazi";
            this.age=12;
            this.getName=function(){
                console.log("我是打酱油的");
            }
        }
        A.prototype.sex="nan";
        var a=new A();
        for(pro in a){
            if (a.hasOwnProperty(pro)) {//此时不打印原型中的方法
                console.log(a[pro]);
            }
        }
function A(){
            this.name="wangmazi";
            this.age=12;
            this.getName=function(){
                console.log("我是打酱油的");
            }
        }
        function B(){

        }
        A.prototype=new B();
        var a=new A();
        console.log(A.prototype instanceof B);//true
                console.log(a instanceof B);//true
上一篇 下一篇

猜你喜欢

热点阅读