Вы находитесь на странице: 1из 11

Object Oriented Programming

JavaScript is an Object Oriented Programming (OOP) language. An OOP language allows you to define your own objects and make your own variable types. An object is just a special kind of data. An object has properties and methods.

Properties & Methods

Properties are the values associated with an object.


<script type="text/javascript"> var txt="Hello World!"; document.write(txt.length); </script>

Methods are the actions that can be performed on objects.


<script type="text/javascript"> var str="Hello world!"; document.write(str.toUpperCase()); </script>

JS Array

Syntax: var myCars=new Array();


<html> <body> <script type="text/javascript"> var x; var mycars = new Array(); mycars[0] = "Saab"; mycars[1] = "Volvo"; mycars[2] = "BMW"; for (x in mycars) { document.write(mycars[x] + "<br />"); } </script> </body> </html>

Array Object Methods


Method
concat() join() pop() push()

Description
Joins two or more arrays and returns the result Puts all the elements of an array into a string. The elements are separated by a specified delimiter Removes and returns the last element of an array Adds one or more elements to the end of an array and returns the new length Reverses the order of the elements in an array Removes and returns the first element of an array Returns selected elements from an existing array Sorts the elements of an array Removes and adds new elements to an array Represents the source code of an object 1 Converts an array to a string and returns the result Adds one or more elements to the beginning of an array and returns the new length

reverse() shift() slice() sort() splice() toSource() toString() unshift()

String Object Methods


Method
anchor() big() blink() bold() charAt() charCodeAt() concat() fixed() fontcolor() fontsize() fromCharCode() indexOf() italics()

Description
Creates an HTML anchor Displays a string in a big font Displays a blinking string Displays a string in bold Returns the character at a specified position Returns the Unicode of the character at a specified position Joins two or more strings Displays a string as teletype text Displays a string in a specified color Displays a string in a specified size Takes the specified Unicode values and returns a string Returns the position of the first occurrence of a specified string value in a string Displays a string in italic

Method
lastIndexOf() link() match() replace() search() slice() small() split() strike() sub()

Description
Returns the position of the last occurrence of a specified string value, searching backwards from the specified position in a string Displays a string as a hyperlink Searches for a specified value in a string Replaces some characters with some other characters in a string Searches a string for a specified value Extracts a part of a string and returns the extracted part in a new string Displays a string in a small font Splits a string into an array of strings Displays a string with a strikethrough Displays a string as subscript

Method
substr() substring() sup() toLowerCase() toUpperCase() toSource()

Description
Extracts a specified number of characters in a string, from a start index Extracts the characters in a string between two specified indices Displays a string as superscript Displays a string in lowercase letters Displays a string in uppercase letters Represents the source code of an object

JS Form Validation
JavaScript can be used to validate data in HTML forms before sending off the content to a server. Form data that typically are checked by a JavaScript could be:
has the user left required fields empty? has the user entered a valid e-mail address? has the user entered a valid date? has the user entered text in a numeric field?

<html> <head> <script type="text/javascript"> function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") { alert(alerttxt);return false; } else { return true; } } } function validate_form(thisform) { with (thisform) { if (validate_required(email,"Email must be filled out!")==false) {email.focus();return false;} } } </script></head> <body> <form action="submit.htm" onsubmit="return validate_form(this)" method="post"> Email: <input type="text" name="email" size="30"> <input type="submit" value="Submit"> </form> </body></html>

To check for valid email:


function validate_email(field,alerttxt) { with (field) { apos=value.indexOf("@"); dotpos=value.lastIndexOf("."); if (apos<1||dotpos-apos<2) {alert(alerttxt);return false;} else {return true;} } }

Вам также может понравиться