javascript-objects-tutorial Javascript Objects Training Material Pdf. Javascript Objects Tutorial for beginners with Example
What are JavaScript Objects?
JavaScript is an Object Oriented Programming (OOP) language. A programming language can be called object-oriented if it facilitates four basic capabilities to developers −
- Aggregation – It is the capability to store one object inside another object.
- Encapsulation − It is one of the main concept in OOPS. It is the capability to store relevant information, whether methods or data, together in an object.
- Polymorphism – It is the capability to write one function or method that works for variety of ways. In other words, it is the ability to take more than one form.
- Inheritance – It is the capability of a class to depend upon another class (or number of classes) and inherit some of its methods and properties. In other words, it is the property of inheriting one class properties into another.
Objects are collection of attributes. If an attribute consists a function, in that case it is considered to be a method of the object, otherwise the attribute is considered to be a property. Properties are variables those are accessible through their object whereas methods are functions that are accessible through their object.
What are Object Properties?
An Object has some properties associated with it . The Object properties define the features of an object. Object properties are generally variablesthat are used inside the object’s methods, but can also be used as globally visible variables that are used across the page.
Below is the syntax for adding a property to an object −
objectName.objectProperty=propertyValue;
For instance − Thebelow code explains how to get the title of the document using the “title” property of the document object.
Along with the properties, Objects can have methods also. These methods are the functions which works with the object’s data. There is a small difference between a function and a method – a function is a standalone unit of block code whereas a method is attached to an object using this keyword to refer the current object.
Methods will take care of all the actions starting from displaying the object contents on the screen to performing complicated mathcalculations on a group of local properties and parameters.
For instance − We can use write() method of document object to write content on the document. Below is a simple example to show how to use it.
In JavaScript, in addition to predefined objects we can also have user-defined objects. The user-defined objects are represented by an object called Object.
What is the JavaScript new Operator?
In order to create an instance of an object this new operator is used. To create an object, one can usenew operator followed by the constructor method.
In the below example, the constructor methods are Object(), Array(), and Date(). The constructors used in the below example are predefined JavaScript functions.
What is the JavaScript Object() Constructor?
A constructor is a function that creates and initializes an object. JavaScript facilitates a special constructor function called Object() to build the object. The value returned from thethe Object() constructor is stored in a variable.
This variable holds a reference to the new object. The properties assigned to the object are variables and are defined with the var keyword.
Example 1
Look at the below example; it illustrates how to create an Object.
Output
userfiles/jso1 JavaScript Objects Output” width=”606″ height=”57″ JavaScript Objects Output”>
Example 2:
This example explains the creation of an object with a User-Defined Function. Here, for refering to an object that is passed to a function this keyword is used.
Output:
userfiles/jso2 JavaScript Objects Output” width=”605″ height=”51″ JavaScript Objects Output”>
How to define methods for a JavaScript Object?
The above discussed examples,explains how the constructor creates the object and assigns properties to it. We have to assign methods to the object for completing the definition of an object..
Example
Look at the below example; it explains how to add a function along with an object.
Output:
userfiles/jso3 JavaScript Objects Output” width=”594″ height=”79″ JavaScript Objects Output”>
What is JavaScript ‘with’ Keyword for?
The ‘with’ keyword provides a sort of shortcut way for referencing an object’s methods or properties.
The object defined as an argument to with makes it as a default object for the span of the block that follows. We can usemethods and properties for the object even without naming the object. Using Withkeyword you can make JavaScript programming very easier.
Syntax
Below is the syntax for with object −
Example
Try the below example.
Output
userfiles/jso4 JavaScript Objects Output” width=”607″ height=”74″ JavaScript Objects Output”>
What are JavaScript Native Objects?
JavaScript has many built-in or native objects. These objects have no restrictions with browser versions and alsothey are platform independent . They can beaccesed anywhere in your program and will work in same way irrespective of browser and operating system.
Below are the list of all impotant JavaScript Native Objects –
- JavaScript Number Object
- JavaScript Boolean Object
- JavaScript String Object
- JavaScript Array Object
- JavaScript Date Object
- JavaScript Math Object
- JavaScript RegExp Object
Let us discuss each of them in detail in the upcoming chapters.
Javascript Objects Tutorial Topics Updated Soon ********