var log = function(msg) { console.log(msg); };
var obj1 = {a: 1, b: 2, c: 3};
/* // all objects have this method, but it's polluted with itself 'allKeys'
// be careful when creating dictionaries
Object.prototype.allKeys = function() {
var result = [];
for (var key in this) {
result.push(key);
}
return result.join(', ');
}
log( obj1.allKeys() ); // return: 'a, b, c, allKeys' */
// ^ could be rewritten with 'defineProperty' to avoid method 'allKeys' in properties
// USE 'definePrototype' to add not-enumerable properties to Object
// not-enumerable - not seen by for...in...
Object.defineProperty(Object.prototype, 'allKeys', {
value: function () {
var result = [];
for (var key in this) {
result.push(key);
}
return result.join(', ');
},
writable: true,
enumerable: false, // makes property hidden for for..in !!!
configurable: true
});
log('Properties list by method:');
log( obj1.allKeys() );
function allKeys(obj) {
var result = [];
for (var key in obj) {
result.push(key);
}
return result.join(', ');
}
log('Properties list by function:');
log( allKeys(obj1) );
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.