IT. Expert System.

Java Script

Objects


Objects

JavaScript is an Object Oriented Programming (OOP) language allowing the programmer to define custom objects and make custom variable types. An object has properties and methods.

Properties

Properties are the values associated with an object.

The syntax for accessing a property of an object is:

objName.propertyName

The following example demonstrates the use of the length property of the String object to return the number of characters in a string:

<script>
var s="Hello JavaScript";
document.s(msg.length);
</script>

This produces the following result:

Methods

Methods are the actions that can be performed on objects.

The syntax for calling a method of an object is:

objName.methodName()

Parameters for the method are passed between the parentheses.

Properties can be added to an object simply by giving it a value. A car object can can have properties like make, model, year, color as follows:

var carObj = new Object();
carObj.make = "Toyota";
carObj.model = "MR2 Spyder";
carObj.year = 2001;
carObj.color = "Red";

document.write(carObj.model);

This produces the following result:


The following example demonstrates the use of the toUpperCase() method of the String object to display a string in uppercase letters:

<script>
var s="Hello JavaScript";
document.write(s.toUpperCase());
</script>

This produces the following result:

Creating an Object

There are two different ways to create a new object:

1. Create a direct instance of an object

The following example creates an instance of an object and adds properties to it:

var carObj = new Object();
carObj.make = "Toyota";
carObj.model = "MR2 Spyder";
carObj.year = 2001;
carObj.color = "Red";

To add a method to the car object:

carObj.go = go;

2. Create a template of an object

The template defines the structure of an object:

function car(make,model,
  year,color)
{
  // Assign the properties
  this.make = make;
  this.model = model;
  this.year = year;
  this.color = color;
  this.speed = 0;

  // Assign a method
  this.go = go;
}

The template is just a function that assigns values to this.propertyName. The "this" refers to the instance of the object at hand.

Methods are just functions attached to objects. Because the go()' function was assigned above, it must be declared:

function go(speed)
{
  this.speed = speed;
}

The go() function sets the speed. JavaScript knows which car by using "this.". Therefore, to set the speed of the car:

carObj.speed(120);

Once template function is complete, new instances of the object can be created, like this:

carObj = new car("Toyota", "MR2 Spyder", 2001, "Red");


Content

Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)

SQL

HTML

PHP

CSS

Java Script

MYSQL

JQUERY

VBS

REGEX

C

C++

C#

Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

License.
All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 2140 / . Delta: 0.03245 с