javascript学习笔记 (一)-函数基础

正则表达式验证表单
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> function validateForm(){
var email = document.forms.tutform.elements.email.value;
if ( ! ( /^ [a - zA - Z][\w\. - ] * [a - zA - Z0 - 9 ]@[a - zA - Z0 - 9 ][\w\. - ] * [a - zA - Z0 - 9 ]\.[a - zA - Z][a - zA - Z\.] * [a - zA - Z]$ / .test(email))){
//上面的也可以用if(!email.match(...);
//这里的正则好像不怎么好用,我给从写了个 /^\s*([A-Za-z0-9_-]+(\.\w+)*@(\w+\.)+\w{2,3})\s*$/;
alert('Pleaseenteravalide
- mailaddress');
return false ;
}
return true ;
}


定义一个函数,需要前三个参数,其余7个是可选的
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> function myFunction(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){
// dostuffwitharg1
// dostuffwitharg2
// dostuffwitharg3
if (arg4){
// dostuffwitharg4
}
if (arg5 && arg6 && arg7){
// dostuffwitharg5,arg6andarg7
if (arg8){
// dostuffwitharg8
}
}
if (arg9 || arg10){
// dostuffwitharg9orarg10
}
}

如果参数没有传值则为"undefine",这代表对象不存在。在判断语句中,系统可以把它转换为布尔值"false"。

函数参数中可以传递函数
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < scripttype = " text/javascript " >
function multiply(){
var out = 1 ;
for ( var i = 0 ;i < arguments.length;i ++ ){
out
*= arguments[i];
}

return out;
}

function add(){
var out = 0 ;
for ( var i = 0 ;i < arguments.length;i ++ ){
out
+= arguments[i];
}

return out;
}

function doAction(action){
alert(action(
1 , 2 , 3 , 4 , 5 ));
}

</ script >

< buttononclick = " doAction(multiply) " > TestMultiply </ button >
< buttononclick = " doAction(add) " > TestAdd </ button >



若你不清楚函数有多少个参数,可以使用function内置的"arguments"对象,它存在与每个函数里面。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> function myFunction(){
for ( var i = 0 ;i < arguments.length;i ++ ){
alert(arguments[i].value);
}
}

函数中可以包含函数,这为实现js的仿OO提供了基础。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> function myFunction(){
function nestedFunction1(arg1,arg2,arg3){
alert(arg1
+ arg2 + arg3);
}

var nestedFunction2 = function (arg1,arg2,arg3){
alert(arg1
+ arg2 + arg3);
}

var nestedFunction3 = new Function('arg1,arg2,arg3','alert(arg1 + arg2 + arg3);');
}

函数定义有很多种,第三中定义方式很少用,但是很有用。你可以使用字符串定义函数。

字符串
转义字符 \
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> var n = " Thedogtookit'sboneoutside " ;
var n = 'Thedogtookit\'sboneoutside';

两者的效果是一样的
常用字符串函数
IndexOf 返回包含指定字符串在另外一个字符串第一次出现的位置,如果不存在则返回-1
lastIndexOf 返回包含指定字符串在另外一个字符串最后一次出现的位置,如果不存在则返回-1
charAt 字符串中指定位置的字符值
substring 获取字符串两个索引间的值
substr 功能和substring类似 第二个参数获取的是所需字符串的长度
例如
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> alert('ThisisaTest'.indexOf('T')); // 0
alert('ThisisaTest'.lastIndexOf('T')); // 10
alert('ThisisaTest'.charAt( 5 )); // i
alert('ThisisaTest'.length); // 14
alert('ThisisaTest'.substring( 5 , 9 )); // isa
alert('ThisisaTest'.substr( 5 , 9 )); // isaTest
alert('ThisisaTest'.toUpperCase()); // THISISATEST
alert('ThisisaTest'.toLowerCase()); // thisisatest


eval 将传入的字符串作为javascript代码执行
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> eval( " alert('Hello,World!') " );


数字
NaN 代表 "not a number"
常用函数
parseInt ,parseFloat,toString

数组
两种定义数组的方式
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> var students = new Array();

students[
0 ] = 'Sam';
students[
1 ] = 'Joe';
students[
2 ] = 'Sue';
students[
3 ] = 'Beth';

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> var students = ['Sam','Joe','Sue','Beth'];


数组的每个项可以包含任何类型的其他项,如字符串,数字,对象,函数,甚至数组.
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> var spreadsheet = [
['A1','B1','C1','D1'],
['A2','B2','C2','D2'],
['A3','B3','C3','D3'],
['A4','B4','C4','D4']
];

通过如下代码访问
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> var col2 = spreadsheet[ 1 ];
alert(col2[
2 ]);
// or
alert(spreadsheet[ 1 ][ 2 ]);


在数组的末尾加入一个新的项
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> var students = ['Sam','Joe','Sue','Beth'];

students[
4 ] = 'Mike';
students[students.length]
= 'Sarah';
students.push('Steve');

// wenowhaveanarraywith7elements:['Sam','Joe','Sue','Beth','Mike','Sarah','Steve']


splice函数的作用很大 可以用来任意添加或删除数组元素 下面示例用来添加和删除数组项.splice接收了两个参数:起始索引,移除项的数目 .
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> var students = ['Sam','Joe','Sue','Beth'];

function addStudent(name){
students.push(name);
}

function removeStudent(name){
for ( var i = 0 ;i < students.length;i ++ ){
if (students[i].toLowerCase() == name){
students.splice(i,
1 );
break ;
}
}
}


我们经常需要将数组转为字符串或字符串转为数组
提供了两个函数
join:将数组转为字符串 每个项之间用一个字符串连接起来
split:将字符串转为数组
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> var myString = 'applesaregood for yourhealth';

var myArray = myString.split('a'); // webreakmyStringapartonevery'a'found.
alert(myArray.join(',')); // wejoinmyArraybacktogetherwithacommasoyoucanseeeachitem
alert(myArray.join('a')); // nowwejoinmyArraybacktogetherwithan'a'sowegetouroriginalstringback

还有两个有用的数组函数
pop:移除最后项且返回该项
shift:移除起始项且返回该项
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> var students = ['Sam','Joe','Sue','Beth'];

while (students.length > 0 ){
alert(students.pop());
}

这样数组会清空,还有一种更简洁的清空数组方式
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> students.length = 0


for语句
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> var students = ['Sam','Joe','Sue','Beth'];

students['Sam']
= 90 ;
students['Joe']
= 85 ;
students['Sue']
= 94 ;
students['Beth']
= 82 ;

alert('Thereare'
+ (students.length) + 'students:' + students.join(','));
for ( var i = 0 ;i < students.length;i ++ ){
alert(students[i]
+ " 'sgradeis: " + students[students[i]]);
}

for in语句实现相同的功能
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> var grades = [];

grades['Sam']
= 90 ;
grades['Joe']
= 85 ;
grades['Sue']
= 94 ;
grades['Beth']
= 82 ;

for (student in grades){
alert(student
+ " 'sgradeis: " + grades[student]);
}

你可能感兴趣的:(JavaScript,正则表达式,OO)