Objects in JavaScript - Programming: Java, JavaScript Coding For Beginners – Learn In A Day (2015)

Programming: Java, JavaScript Coding For Beginners – Learn In A Day (2015)

Chapter 4. Objects in JavaScript

Java Script- objects:

Objects in JavaScript consist of attributes. An attribute is considered a method, if it has a function, or else an attribute is considered a property.

Object Properties:

The properties of the object can be in any of the data types.

The following syntax is used when one has to add property to an object:

objectName.objectProperty = propertyValue;

Example:

The next example showcases how to use the "title" property of the document object:

var strg = document.title;

Object Methods:

The functions allowing the object to do a particular task.

Example:

The following example shows how the write() method is used:

document.write("This is text");

User-Defined Objects:

The new Operator:

The new operator helps in creating instances of an object. For creating an object, the constructor method follows the new operator.

In the below given example, Object(), Array(), and Date() are the constructor methods, which are in-built functions of JavaScript.

var emp = new Object();

var textbooks = new Array("C", "Python", "Java");

var day = new Date("August 21, 1987");

The Object() Constructor:

An object can be created and initialized by a constructor. Object() is a special constructor function that helps to build an object.

The following example shows to create an object:

<html>

<head>

<title>objects defined by the user </title>

<script type="text/javascript">

var book = new Object(); // Creates the object

book.subject = "Perl"; // Assign properties to the object

book.author = "Yashwanth";

</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>

JavaScript Native Objects:

JavaScript consists of many in-built native objects. Their behavior is not affected by the browser or the platform on which they run.

The following is a list of most widely used native objects in JavaScript:

1. Number Object

2. Boolean Object

3. String Object

4. JavaScript Math Object

The Number Object:

The number objects in JavaScript are used to represent numerical values. It can hold both integers and floating point values.

Whenever encountered with numerical values, the web browser converts those values into the number class instances automatically.

Syntax:

The syntax for creating a number object is as follows:

var value = new Number(number);

Number Methods

The following are the methods that are consisted in the number object.

Method

Description

constructor()

This method is used to return the function that created the instance of the object.

toExponential()

This methods results in the display of a standard range number in the exponential form.

toLocaleString()

This method takes a number and returns a string version for the number. It is done in a format that varies with the locale settings of the web browser.

toPrecision()

This method defines the number of digits to be displayed in a given number. This includes the digits that are present on either side of the decimal.

toString()

This method helps to convert a number into a string value.

valueOf()

This method represents the value of the number.

The Boolean Object:

The Boolean object is used to represent a value that can be either a ‘True” value or a ‘False’ value.

Syntax:

A boolen object can be created using the following syntax:

var bval = new Boolean(value);

The Boolean object’s initial value is false if the value parameter holds any of the following values: empty string, null, 0, Nan, -0, null or undefined.

Boolean Methods:

The following are the methods that are consisted in the Boolean object.

Method

Description

toSource()

This method is used to return a string which consists of the Boolean object’ source. An equivalent object can be created using that string.

toString()

This method is used to return a ‘true’ string or a ‘false’string based on the object’s value.

valueOf()

This method is used to return the Boolean object’s primitive value.

The String Object:

The string object allows the user to work with a group of characters and wraps the string primitives of JavaScript with the helper methods.

Syntax:

A String object can be created using the following syntax

var value = new String(string);

The string parameter represents a group of properly encoded characters.

String Methods

The following are the methods that are consisted in the String object.

Method

Description

charCodeAt()

This method is used to return a number, that indicates a character’s Unicode value at a given index.

concat()

This method is used to combine characters of two strings and return a newly created string.

match()

This method is used for matching a string with a regular expression.

toLocaleLowerCase()

Used to convert a string into lower case while maintaining the present locale.

toLocaleUpperCase()

Used to convert a string into upper case while maintaining the present locale.

tolowerCase()

Returns a string in which all uppercase letters are converted into lowercase letters

toupperCase()

Returns a string in which all lowercase letters are converted into uppercase letters.

Math Object:

It allows the user to perform many common mathematical calculations. The object methods are called by writing the object’s name and then a dot and followed by the method’s name. The argument(s) are written in the method parenthesis.

Ex: document.writeln(Math.sqrt(81));

Following are some widely used functions of Math object:

Methods

Description

abs(x)

Absolute value of x

ceil(x)

Rounds x to the smallest integer not less than x.

round(x)

Round x to closest integer.

pow(x , y)

X raised to the power of y

floor(x)

Rounds x to the largest integer not greater than x.

Conclusion

We’ve come to the end of the book on learning the basics of Java and JavaScript programming.

I want to thank you for purchasing this book. I hope you found this book useful and it helped you in understanding the concepts of programming using Java and Java Script. Being a high-level programming language, it makes it easy to learn the basics and then start writing programs on your own.

No language is rocket-science if studied properly. So is the case with the two languages covered in this book. Dive in and know the basics for these languages.

Thank you!