Javascript - (Object) Property

About

Property Management of an object

A property has :

Management

Exist

var myObj = {myProp:3} ;
if ("myProp" in myObj) {
    console.log("Yes, i have that property");
} 
if (!("myFakeProp" in myObj)) {
    console.log("No, i haven't that property");
}

Computed property names (ES2015)

Property names can be dynamic

var param = 'size';
var config = {
  [param]: 12,
  ['mobile' + param.charAt(0).toUpperCase() + param.slice(1)]: 4
};

console.log(config); // {size: 12, mobileSize: 4}

Delete property

en-US/docs/Web/JavaScript/Reference/Operators/delete

delete object.property
delete object['property']

Dynamic Object Property access

var obj = { 
     a: "hello world", 
     b: 42 
}; 

var b = "a"; 

console.log(obj[ b]); // "hello world" 
console.log(obj["b"]); // 42

Object or prototype Property

A property may be owned by its prototype.

// An object
var person = {
   name: "Nicolas", 
   length: 180
};

// A constructor
function ColoredPerson() {
    // Rode piet !
    this.color = "red";
}

// Set the prototype to be the person object (dynamic inheritance)
// All new ColoredPerson must inherit its properties.
ColoredPerson.prototype = person;

// Create a new object
var coloredPerson = new ColoredPerson();

// Loop through the properties
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);
}

Loop (for property in object)

en-US/docs/Web/JavaScript/Reference/Statements/for...in.

The loop iterate over all enumerable properties of the object itself and those the object inherits from its constructor's prototype (properties closer to the object in the prototype chain override prototypes' properties).

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 + ".");
}

Length

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

Audit

to audit if a static property value was accessed such as the userAgent, you can define a getter

Example: if a script the userAgent property, the property window.navigator.sniffed is set to true

window.navigator.__defineGetter__('userAgent', function() {
    window.navigator.sniffed = true;
    return userAgent;
});





Discover More
DOM - Select an element by its id (with javascript and css example)

This article shows 1001 ways on how to select an element via its id attribute
Javascript - (Object) Key

Object key management. A key is also known as a property name :) Check all properties (also the inherited one) check only the direct properties of the object and not the inherited one...
Javascript - Array

array in Javascript can contain any type of data. Different types of values can be stored within the same array Because arrays are special objects (as typeof implies), they can also have properties, including...
Javascript - For Statement

The for iterator statement array iteration The colors Reference/Statements/for...in. array The loop iterate over all enumerable properties of the object itself and those the object inherits...
Javascript - JQuery

Jquery is a javascript library that principally implements higher function of the DOM API in order to manipulate HTML page. As it offers a plugin framework, it's also a great...
Javascript - Method

Methods in JavaScript are nothing more than object properties that happen to be functions. Basic: same as with
Javascript - Object

object in javascript An object is a data type in Javascript. An object follows a json data structure with key that holds property value. The value may be of a variable or a function In JavaScript, a...
Javascript - Object Accessor (getter / setter)

in javascript of property Since ECMAScript 5. If your are in control of the object, you can audit if a property was accessed (otherwise you can audit with a proxy) Set...
Data System Architecture
Key Value (or Property)

Key value data (known also as Property) are value that have a direct relation with their key. They can be seen as a set of data modeling attribute. This kind of data is stored: in code as: an collection...
Object - Property value

of a property of an object



Share this page:
Follow us:
Task Runner