1

All I can see are indexes and values. Other properties like length or callee are not displayed. How to hide properties from console.log()? And how to see all the properties?

For example:

function test(){
    console.log(arguments);
    console.log(arguments.length);
}

test(1,2,3,4,5);

The output is { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 } and 5

Actually there is length property in arguments but I cannot see in console.log(arguments).

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Matthew Ma
  • 45
  • 6

1 Answers1

2

Because arguments.length property is non-enumerable.

You can define a property on an object and set its enumerable attribute to false, like this

var obj = {};

Object.defineProperty(obj, "name", {
    "value": "a",
    enumerable: false
});

console.log(obj);
// {}

You can check the same with Object.prototype.propertyIsEnumerable function, like this

function testFunction() {
    console.log(arguments.propertyIsEnumerable("length"));
}

testFunction();

would print false, because length property of arguments special object is not enumerable.

If you want to see all the properties, use the answers mentioned in this question. Basically, Object.getOwnPropertyNames can enumerate even the non-enumerable properties. So, you can use that like this

function testFunction() {
    Object.getOwnPropertyNames(arguments).forEach(function (currentProperty) {
        console.log(currentProperty, arguments[currentProperty]);
    });
}

testFunction();

this would print the length and the callee properties.

Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • Yes, of course. I'm trying your code. I found Object.defineProperty works well in Node.js. I cannot see property name (the default value of enumerable is false). But in chrome, I can still see {name:a}. – Matthew Ma Apr 15 '15 at 15:33
  • @MatthewMa Oh that.... Please check [this discussion](http://stackoverflow.com/q/12146473/1903116) – thefourtheye Apr 15 '15 at 15:35