Matthew Ma
Matthew Ma

Reputation: 45

Why I am not able to see `length` property of arguments objects in `console.log` ouput?

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).

Upvotes: 1

Views: 55

Answers (1)

thefourtheye
thefourtheye

Reputation: 239573

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.

Upvotes: 2

Related Questions