Returns the names of the enumerable properties and methods of an object.
SyntaxObject.keys( object )
An array that contains the names of the enumerable properties and methods of the object.
ExamplesThe following example creates an object that has three properties and a method. It then uses the keys method to get the properties and methods of the object.
function Pasta(grain, width, shape) {
this.grain = grain;
this.width = width;
this.shape = shape;
this.toString = function () {
return (this.grain + ", " + this.width + ", " + this.shape);
}
}
var spaghetti = new Pasta("wheat", 0.2, "circle");
var arr = Object.keys(spaghetti);
document.write (arr);
The following example displays the names of all enumerable properties that start with the letter “g” in the Pasta object.
function Pasta(grain, width, shape) {
this.grain = grain;
this.width = width;
this.shape = shape;
}
var polenta = new Pasta("corn", 1, "mush");
var keys = Object.keys(polenta).filter(CheckKey);
document.write(keys);
function CheckKey(value) {
var firstChar = value.substr(0, 1);
if (firstChar.toLowerCase() == "g")
return true;
else
return false;
}
Remarks
The keys method returns only the names of enumerable properties and methods. To return the names of both enumerable and non-enumerable properties and methods, you can use Object.getOwnPropertyNames Function.
For information about the enumerable attribute of a property, see Object.defineProperty Function and Object.getOwnPropertyDescriptor Function.
ExceptionsIf the value supplied for the object argument is not the name of an object, a TypeError exception is thrown.
See also Other articles AttributionsMicrosoft Developer Network: Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4