contents   index   previous   next



Object operator

 

The object operator is a period, ".". This operator allows properties and methods of an object to be accessed and used. For example, Math.abs() is a method of the Math object. It may be accessed as follows:

 

var AbsNum = Math.abs(-3)

 

The variable AbsNum now equals 3. The variable AbsNum is an instance of the Number object, not an instance of the Math object. Why? It is assigned the number 3 which is the return of the Math.abs() method.

 

The Math.abs() method is a static method, that is, it is used directly with the Math object instead of an instance of the object. Many methods are instance methods, that is, they are used with instances of an object instead of the object itself. The String substring() method is an instance method of the String object. An instance method is not used with an object itself but only with instances of an object. The String substring() method is never used with the String object as String.substring(). The following fragment declares and initializes a string variable, which is an instance of the String object, and then uses the String substring() method with this instance by using the object operator.

 

var s = "One Two Three";

var new = s.substring(4,7);

 

The variable s is an instance of the String object since it is initialized as a string. The variable new now equals "Two" and is also an instance of the String object since the String substring() method returns a string.

 

The main point here is that the period "." is an object operator that may be used with both static and instance methods and properties. A method or property is simply attached to an appropriate identifier using the object operator, which then accesses the method or property.

 


Mathematical operators