Javascript objects

You,js

Javascript Object and keys

All javascript objects are hash tables which are key value pairs.

someObj={ name: 'Sudhi', weight: 90, ID : 'St047319'};
 
//to see the keys of an object, one could use the Object.keys(...) method
Object.keys(someObj)

Output:

[ 'name', 'weight', 'ID' ]

This means, they "keys" are simply the member variables.

Though one would not do this, but it does happen once a while, and look at the object below, which is a combination of values and functions (OOP style)

anotherObj={ name: 'Sudhi', weight: 90, ID : 'St047319', registerFunc: function (v) {console.log("registering " + v) }}
 
Object.keys(anotherObj);

Output:

['name', 'weight', 'ID', 'registerFunc' ]

Lets try this with a more complex object contain sub-objects or child-objects...

 table = {
    row1: {col1: 'A', col2: 'B', col3: 'C' },
    row2: { col1: 'D', col2: 'A', col3: 'F' },
    row3: {col1: 'E',col2: 'G',col3: 'C'}};
 
Object.keys(table);
 
Object.keys(table.row1)

Output:

[ 'row1', 'row2', 'row3' ]

[ 'col1', 'col2', 'col3' ]

An empty object can be validly queried for keys, and it will rightly return an empty array

var v1 = {}; //create an empty object like so
var v2 = new Object; //or like so
 
//and enquire the keys
Object.keys(v1);
Object.keys(v2);

Output:

[]
[]

Now for, - as per beholder's eyes - some messups or javascript specific beauty .

function hello(x) {console.log('Hello there, ' + x);}