How do I access properties of a javascript object if I don't know the names?

前端 未结 8 1445
囚心锁ツ
囚心锁ツ 2020-11-28 18:49

Say you have a javascript object like this:

var data = { foo: \'bar\', baz: \'quux\' };

You can access the properties by the property name:

8条回答
  •  星月不相逢
    2020-11-28 19:30

    You often will want to examine the particular properties of an instance of an object, without all of it's shared prototype methods and properties:

     Obj.prototype.toString= function(){
            var A= [];
            for(var p in this){
                if(this.hasOwnProperty(p)){
                    A[A.length]= p+'='+this[p];
                }
            }
    
        return A.join(', ');
    }
    

提交回复
热议问题