Javascript - (Object) Key

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript) > ECMAScript - Grammar Standard > Javascript - Object

1 - About

Object key management.

A key is also known as a property name :)

3 - Management

3.1 - Name

A property name must not begin with a number

3.2 - Loop Keys: For ... in

var person = {name: "Nicolas", length: 180};
 
function ColoredPerson() {
    // Rode piet !
    this.color = "red";
}
ColoredPerson.prototype = person;
 
var coloredPerson = new ColoredPerson();
 
var propSource;
for (var prop in coloredPerson) {
    if (coloredPerson.hasOwnProperty(prop)) {
        propSource = "object";
    } else {
        propSource = "prototype";
    }
    console.log("The property " + prop + " (value:" + coloredPerson[prop] + ") is a property from the " + propSource + ".");
}

Advertising

3.3 - Keys

3.3.1 - Object.keys

Reference/Global_Objects/Object/keys get the key of the object (not from the prototype).

Object.keys(obj)

Example:

var person = {name: "Nicolas", length: 180};
 
function ColoredPerson() {
    // Rode piet !
    this.color = "red";
}
ColoredPerson.prototype = person;
 
var coloredPerson = new ColoredPerson();
 
console.log("Object.keys will not list the prototype property");
var keys = Object.keys(coloredPerson);
for (var i = 0; i < keys.length; i++) {
    console.log("The property " + keys[i] + " (value:" + coloredPerson[keys[i]] + ") is a property from the objects.");
}

3.3.2 - Length

console.log("The number of key/properties in the global object is "+ Object.keys(this).length);

web/javascript/es/object/key.txt · Last modified: 2019/02/16 17:40 by gerardnico