• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

JavaScript Objects & Properties

JavaScript Objects Overview

JavaScript is an object Oriented Programming(OOP) language.A programming language can be called object oriented if it provides four basic capabilities to developers:

Encapsulation, the capability to store related information, whether data or methods, together in an object

Aggregation, the capability to store one object inside of another object.

The capability of a class to rely upon another class(or number of classes) for some of its properties and methods

The capability to write one function or method that works in a variety of different ways.

Objects are composed of attributes. If an attribute contains a function, it is considered to be a method of the object otherwise, the attribute is considered a property.

JavaScript Object Properties

JavaScript Object  Properties can be any of the three primitive data types or any of the abstract data types, such as another object. JavaScript Object properties are usually variables that are used internally in the objects methods, but can also be globally visible variables that are used internally in the objects methods, but can also be globally visible variables that are used throughout the page.

The syntax for adding a property to an object is

ObjectName.objectProperty=propertyValue;

Example

Following is a simple example to show how to get a document title using “title” property of document object:

Var str=document.title;

JavaScript Object Methods

The methods are functions that let the object to do something be done to it. There is little difference between a function and a method ,except that a function is a standalone unit of statements and a method is attached to an object and can be referenced by the this keyword.

Methods are useful for every thing from displaying the contents of the object to the screen to performing complex mathematical operations on a group of local properties and parameters.

Example

Following is a example to show how to sue write() method of document object to write any content on the document:

Document. write{“This is test”};

User-Defined Objects

All user-defined objects and built in objects are descendants of an object called Object.

The new Operator

The new operator is used to create an instance of an object.To create an object,the new operator is followed by the constructor method.

In the following example,the constructor methods are Object{},Array{} and Date{}.These construcots are built-in JavaScript functions.

Var employees=new Object{};

Var books=new Array{“C++”,”Perl”,”Java”};

Var day=new Date{August 15,1947”};

The Object() Constructor 

A constructor is a function that creates and initializes an object.Java Script provides a special constructor function calle object() to build the object.The return value of the Object() constructor is assigned to a variable.

The variable contains a reference to the new object.The properities assigned to the object arenot variables and are not defined with the var keyword.

Example 1

This example demonstrates how to create an object:
<html>

<head>

<title>User-define objects</title>

<script type=”text/javascript”>

Var book=new Object{};  //Create the object

Book.subject=;Perl”;//Assign properties to the object

Book.author  =”Mohtashim”;

</script>

</head>

<body>

<script type=”text/javascript’>

Document.write{“Book name is: “+book.subject+”,br>”};

Document.write{Book author is:”book.author +”<br>”}’

</script>

</body>

</html> 

 

Example 2

This example demonstrates how to create an object with a User-Defined Function. Here this keyword is used to refer to the object that has been passed to a function:

<html>

<head>

<title>User-define objects</title>

<script type=”text/javascript”>

Var myBook=new book{“Perl”, “Mchtashim”};

Document.write{‘Book title is :”+myBook.title+”<br>”};

Document.write{‘Book author is :”+myBook.author+”<br>”};

</script>

<body>

</html>

Defining Methods for an Object in JavaScript

The previous example demonstrates how the constructor creates the object and assigns properties. But we need to complete the definition of an object by assigning methods to it. 

<html>

<head>

<title>User-define objects</title>

<script type=”text/javascript”>

// Define a function which will work as a method

Function add Price{amount}

This.price=amount;

Function book{title,author}{

This.title=title;

This.auhtor=author;

This.addPrice=addPrice;//Assign that method as property.

</script>

</head>

<body>

<script type=”text/javascrpit”>

var myBook= new book(“Perl”,”Mohtashim”);

myBook.addprice(100);

document.write(“Book title is : “ + myBook.title + “<br>);

document.write(“Book author is : “ + myBook.author + “<br>);

document.write(“Book price is : “ + myBook.price + “<br>);

</script>

</body>

</html>

The with Keyword

The with keyword is used as a kind of shorthand d for referring an objects properties or methods.

The object specified as an argument to with becomes the default object for the duration of the duration of the block that follows. The properties and methods for the object can be used without naming the object.

Syntax

with (object){

properties used without the object name and dot.

}

Example

<html>

<head>

<title>User-defined objects </title>

<script type=”text/javascript”>

//Define a function which will work as a method

function add price(amount)

with(this){

price=amount;

}

}

function book(title, author){

this.title=title;

this.author=author;

this.price=0;

this.addprice=addprice; //Assign that method as property.

}

</script>

</head>

<body>

<script type=”text/javascrpit”>

var myBook= new book(“Perl”,”Mohtashim”);

myBook.addprice(100);

document.write(“Book title is : “ + myBook.title + “<br>);

document.write(“Book author is : “ + myBook.author + “<br>);

document.write(“Book price is : “ + myBook.price + “<br>);

</script>

</body>

</html>

JavaScript Native Objects

JavaScript has several built-in or native objects. These objects are accessible anywhere in your program and will work the same way in any browser running in any operating system.

Here is the list of all important JavaScript Native Objects

JavaScript Number object

JavaScript Boolean object

JavaScript String object

JavaScript Array object

JavaScript Date object

JavaScript Math object

JavaScript RegExp object

JavaScript –The string Object

The String object let’s you work with a series of  characters and wraps Javascrpt’s string primitive data type with a number of helper methods.

Because Javascript automatically converts between string primitives and String objects, you can call any of the helper methods of the String object on a string primitive.

Syntax

Creating a String object

var val =new string(string)

The string parameter is series of characters that has been properly encoded.

String Properties

Here is a list of each property and their description.

 PropertyDescription
ConstructorReturns a reference to the String function that created the object.
lengthReturns the length of the string
prototypeThe prototype property allows you to add properties and methods to an object.

String Methods

Here is a list of each method and its description.

 MethodDescription
charAt()Returns the character at the specified index.
charCodeAt()Returns a number indicating the Unicode value of the character at the given index.
concat()Combines the text of two strings and returns a new string.
indexOf()Returns the index within the calling String Object of the first occurrence of the specified value, or -1 if not found.
lastIndexOf()Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.
localeCompare()Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.
match()Used to match a regular expression against a string.
replace()Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.
search()Execute the search for a match between a regular expression and a specified string.
slice()Extract a section of a string and returns a new string.
spilt()Splits a string object into an array of string by separating the string into substrings.
Summary
Review Date
Reviewed Item
JavaScript Objects & Properties
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on JavaScript Objects & Properties"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.