Functions, Global Functions, and Custom Objects.

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

Functions, Global Functions, and Custom Objects.

Rating:

Functions: Functions can be referred to as a sub-program that can be used in other parts of the program. They perform a specified task as per the statements written in the function block. Functions are invoked with the help of event handlers. The users can use these functions anywhere in the program. Functions are created by using the function keyword.

The syntaxes for creating a function are as follows:

function functionname()
{
        //statements
}


The above syntax of creating the function does not contain any parameter.

function functionname(param1, param2,...)
{
        //statements
}


The above syntax of creating the function contains parameters. A function can have any number of parameters that should be passed in its parentheses.

The function name should begin with an alphabet or underscore. The name of a function cannot begin with a number. This will result in an error. Numbers can only be used in the middle of the function name. The keywords and the special characters such as <, >, period comma, etc, cannot be used in the function name because these are reserved in JavaScript. The functions are invoked by the event handlers of an object.

The syntax of invoking a function is as follows:

<form>
<input type="button" onclick="functionname()">
</form>


The function should be in the same case (uppercase or lowercase) as it has been defined in the script.

The following example will demonstrate the creation and the invocation of a function:

<html>
<head>
<script language="javascript">
function myfunc()
{
       document.write("This is a function")
}
</script>
</head>
<body>
<form>
<input type="button" value="Invoke Function" onclick="myfunc()">
</form>
</body>
</html>


Global Functions: The JavaScript language defines some functions that do not belong to any JavaScript object. These kinds of functions are called global functions or global methods. These functions can be used to perform conversion of datatypes or validation of a value, etc. The following are the global functions used in JavaScript:

The eval function: The eval function is a global function of JavaScript. This function is used to evaluate a JavaScript expression that is passed as a string in its parentheses.

The syntax of using the eval function is as follows:

eval(expr)

The following example will demonstrate the use of the eval function:

<script language="javascript">
function evaldemo()
{
       eval("document.write('This is an example of eval function')")
}
</script>


The eval function can also be used to convert a string to a number. The following example will demonstrate this:

<script language="javascript">
function evaldemo()
{
       var a=eval(prompt("enter a number"))
       var b=eval(prompt("enter a number"))
       var c=a+b
       alert(c)
}
</script>


The escape function: The escape function is a global function that is used to convert the ASCII value of a non-alphanumeric character to its equivalent hexadecimal value preceded by a percent sign.

The syntax of using the escape function is as follows:

escape(string)

The following example will demonstrate the use of the escape function:

<script language="javascript">
function escdemo()
{
       var a="hello world"
       var b=escape(a)
       document.write(b)
}
</script>


The isNaN function: The isNaN function is a global function. It is used to test whether or not an entry is a number. The value returned by this function is boolean. The isNaN function returns true if the value is not a number.

The syntax of using the isNaN function is as follows:

isNaN("string")

The following example will demonstrate the use of the isNaN function:

<script language="javascript">
function isnandemo()
{
       var a=prompt("Enter a number")
       if(isNaN(a))
       {
               alert("This is not a number")
       }
       else
       {
               document.write(a)
       }
}
</script>


The Number function: The Number function is used to convert a string value to a numeric value. If the string value does not contain numbers, it returns NaN (Not a Number).

The syntax of using the Number function is as follows:

Number("string")

The following example will demonstrate the use of the Number function:

<script language="javascript">
function numdemo()
{
       var a=Number(prompt("Enter a number"))
       var b=Number(prompt("Enter a number"))
       var c=a+b
       alert(c)
}
</script>


The parseInt function:

The parseInt function is used to convert a string value to a numeric value. If the string value does not contain numbers, it returns NaN (Not a Number).

The syntax of using the parseInt function is as follows:

parseInt("string")

The following example will demonstrate the use of the parseInt function:

<script language="javascript">
function numdemo()
{
       var a=parseInt(prompt("Enter a number"))
       var b=parseInt(prompt("Enter a number "))
       var c=a+b
       alert(c)
}
</script>


The parseFloat function:

The parseFloat function is used to convert a string value to a numeric value. If the string value does not contain numbers, it returns NaN (Not a Number).

The syntax of using the parseFloat function is as follows:

parseFloat("string")

The following example will demonstrate the use of the parseFloat function:

<script language="javascript">
function numdemo()
{
       var a=parseFloat(prompt("Enter a number"))
       var b=parseFloat(prompt("Enter a number "))
       var c=a+b
       alert(c)
}
</script>


The String function: The String function is used to convert a value or an object to a string.

The syntax of using the String function is as follows:

String(value)

The following example will demonstrate the use of the String function:

<script language="javascript">
function strdemo()
{
       var a=Number(prompt("enter a number"))
       var b=Number(prompt("enter a number"))
       var c=a+b
       alert(c)
       var d=String(a)
       var e=String(b)
       var f=d+e
       alert(f)
}
</script>


The toString function: The toString function is used to convert the content of an object to a string by invoking the function on the object.

The syntax of using the toString function is as follows:

object.toString([radix])

The radix argument is optional. The radix argument accepts the values from 0 to 16 and converts the number according to binary, decimal, hexadecimal, or to another base that is passed as the value of radix.

The following examples will demonstrate the use of the toString function;

<script language="javascript">
function tostrdemo()
{
       var arr=new Array(1,2,3,4,5)
       var str=arr.toString()
       alert(str)
}
</script>


The above program will convert the elements of an array to string.

<script language="javascript">
function radixdemo()
{
       var a=10
       var b=a.toString(2)
       alert(b)
}
</script>


The above program will convert the value of the variable a to its binary equivalent.

The unescape function: The unescape function is the reverse of the escape function. It converts the hexadecimal value of the non-alphanumeric code to its ASCII equivalent.

The syntax of using the unescape function is as follows:

<script language="javascript">
function unescdemo()
{
       var a="hello%20world"
       var b=unescape(a)
       document.write(b)
}
</script>


Custom Objects: An object is a well-defined entity that contains properties and methods. The properties are used to change the state of an object, while the methods are the actions that are used to perform a specified task on an object.

The syntax of using the properties and methods of an object are as follows:

object.propertyname
object.methodname


JavaScript contains the objects that can be used for various purposes such as window, document, form, etc.

A user can create his own objects in JavaScript that can have custom properties and custom methods. These kinds of objects are called user-defined objects or custom objects.

The code for creating a custom object is as follows:

<script language="javascript">
function Student(name,age,address,getName)
{
       this.name=name //Property
       this.age=age //Property
       this.address=address //Property
       this.getName=getName //Method
}
</script>


The code for using the custom object is as follows:

<script language="javascript">
function useobj()
{
       var stu=new Student("John", 20, "California")
       alert(stu.name)
}
</script>


The code for the getName method is as follows:

<script language="javascript">
function getName(name)
{
       this.name=name
       return name
}
</script>


Rating:



Other articles

Click here to Article home

MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW Certification: CIW foundation, CIW Professional, Site Designer    Photoshop ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.
 
HACKER SAFE certified sites prevent over 99.9% of hacker crime.