Full width home advertisement

How To

Tech

JavaScript

Post Page Advertisement [Top]

JavaScript Properties

JavaScript Properties

JavaScript Properties


Properties are the most important part of any JavaScript object.

JavaScript Properties

Properties are the values associated with a JavaScript object.
A JavaScript object is a collection of unordered properties.
Properties can usually be changed, added, and deleted, but some are read only.

Accessing JavaScript Properties

The syntax for accessing the property of an object is:
objectName.property          // person.age
or
objectName["property"]       // person["age"]
or
objectName[expression]       // x = "age"; person[x]

NoteThe expression must evaluate to a property name.

Example 1

person.firstname + " is " + person.age + " years old.";

Example 2

person["firstname"] + " is " + person["age"] + " years old.";


JavaScript for...in Loop

The JavaScript for...in statement loops through the properties of an object.

Syntax

for (variable in object) {
    code to be executed
}
The block of code inside of the for...in loop will be executed once for each property.
Looping through the properties of an object:

Example

var person = {fname:"John", lname:"Doe", age:25};

for (x in person) {
    txt += person[x];
}


Adding New Properties

You can add new properties to an existing object by simply giving it a value.
Assume that the person object already exists - you can then give it new properties:

Example

person.nationality = "English";

NoteYou cannot use reserved words for property (or method) names. JavaScript naming rules apply.


Deleting Properties

The delete keyword deletes a property from an object:

Example

delete person.age;   // or delete person["age"]; 
The delete keyword deletes both the value of the property and the property itself.
After deletion, the property cannot be used before it is added back again.
The delete keyword does not delete inherited properties, but if you delete a prototype property, it will affect all objects inherited from the prototype.

Property Attributes

All properties have name. In addition they also have a value.
The value is one of the property's attributes.
Other attributes are: enumerable, configurable, and writable.
These attributes define how the property can be accessed (is it readable?, is it writable?)
In JavaScript, all attributes can be read, but only the value attribute can be changed (and only if the property is writable).
( ECMAScript 5 has methods for both getting and setting all property attributes)

Prototype Properties

JavaScript objects inherit the properties of their prototype.

JavaScript Methods

Methods are the actions that can be performed on objects.
NoteMethods are just functions defined as object properties.
You can call a method with the following syntax:
objectName.methodName()
This example uses the toUpperCase() method of the String object, to convert a text to uppercase:
var message = "Hello world!";
var x = message.toUpperCase();
The value of x, after execution of the code above will be:
HELLO WORLD!


Adding New Methods

Defining methods to an object is done inside the constructor function:
function person(firstname, lastname, age, eyecolor) {
    this.firstname = firstname; 
    this.lastname = lastname;
    this.age = age;
    this.eyecolor = eyecolor;
    this.changeName = changeName;

    function changeName(name) {
        this.lastname = name;
    }
}
The changeName() function assigns the value of name to the person's lastname property.

Now You Can Try:

myMother.changeName("Doe");
JavaScript knows which person you are talking about by "substituting" this with myMother.

Bottom Ad [Post Page]