Thursday, 3 January 2013

Previous Interview Question Next Interview Question
Home

What is a property in javascript

It is impossible to explain what an object is without explaining what a property is also. Simply put, the variables that makes up an object are called the object's properties. In other words, properties are variables that belongs to an object; thus they are sometimes called member variables. Other than the fact that properties belong to objects, there is little behavioral difference between variables and properties.
Properties have names just like variables do, and in fact follow the same naming rules. However, a property's name alone is insufficient to identify an object's property. This is because properties belong to objects, and the object to which the property belongs must also be specified. In JavaScript, properties are identified by concatenating the object and property names together, with the object name first and the two names separated by a dot (period).
Examples:
  • time . hours
  • time . minutes
  • current_window . width
  • user . password
  • user . name . first *
Consider a variable, called "watch," whose value is an object that represents a stopwatch. If the properties of watch are hours and minutes, then the program fragment below demonstrates how watch's properties can be manipulated so that the object represents a time which is 5 minutes later:
/* Increase minutes by 5: */
watch . minutes += 5;

/* If minutes now exceed 59, then subtract 60 and bump the hours by one: */
if (watch . minutes > 59)
{
        watch . minutes -= 60;
        watch . hours += 1;
}
As you can see, properties are treated just like normal variables. From this point of view, properties are like variables with long, dotted names. On the other hand, you shouldn't forget that properties are components of their objects, and that the objects themselves are also variables.

Previous Interview Question Next Interview Question
Home

0 comments :

Post a Comment