javascript学习笔记:基本语法

来源:互联网 发布:火山移动编程 编辑:程序博客网 时间:2024/04/28 19:46

在HTML中调用外部JS文件的方法:

<html><head><title></title></head><body><script type="text/javascript" src="javascriptDemo.js"></script></body></html>

javascriptDemo.js

//内容越往下越基础//JS中的全局函数var str4="document.write('Demo of eval()'+'<br/>')";eval(str4);//eval()函数能够识别字符串中的JS代码并执行//JS的date对象var date=new Date();document.write("defaul date:"+date+"<br/>");document.write("date after transformed:"+date.toLocaleString()+"<br/>");//JS的array对象var arr1=new Array(1,2,3);var arr2=new Array(4,5,6);document.write("defaul arr1:["+arr1+"]<br/>");document.write("defaul arr2:["+arr2+"]<br/>");document.write("arr1.length:"+arr1.length+"<br/>");//arrayObject.push()链接两个数组document.write("arr1.concat(arr2):["+arr1.concat(arr2)+"]<br/>");//arrayObject.push()是以单个元素为单位添加的,在使用后会返回新数组的长度document.write("arr1.push(4)'s return:"+arr1.push(4,5)+",the new arr is:["+arr1+"]<br/>");//arrayObject.pop()从数组中弹出一个元素并返回这个元素document.write("arr1.pop()'s return:"+arr1.pop()+",the new arr is:["+arr1+"]<br/>");document.write("arr1.reverse:["+arr1.reverse()+"]<br/>");//JS的string对象var str3="Daya";document.write("defaul string:"+str3+"<br/>");document.write("stringObject.length:"+str3.length+"<br/>");document.write("stringObject.fontcolor('red'):"+str3.fontcolor('red')+"<br/>");document.write("stringObject.fontsize(8):"+str3.fontsize(8)+"<br/>");document.write("stringObject.link('Demo.html'):"+str3.link("Demo.html")+"<br/>");document.write("stringObject.sub():"+str3.sub()+"<br/>");document.write("stringObject.sup():"+str3.sup()+"<br/>");//函数function add(a,b){return a+b;}document.write(add(1,2));//匿名函数,与事件有关var add2=function(a,b){return a+b;}document.write(add2(1,2));//利用JS往页面中写入文本document.write("<table border='1' bordercolor='black'>");for(row=1;row<=9;row++){document.write("<tr>");for(col=1;col<=row;col++)document.write("<td>"+col+"*"+row+"="+(row*col)+"</td>");document.write("</tr>");}document.write("</table>");//字符串运算var str1="123";var str2="abc"alert("123(str)+1="+(str1+1));alert("123(str)-1="+(str1-1));alert("abc-1="+(str2-1));//javascript中的循环语句同C语言,数学运算与C类似,区别在于javascript不区分小数与整数var num1=123;var num2=1000;alert(num1+"/"+num2+"="+(num1/num2));//javascript中的5种原始变量类型var str="abc";//字符串var num=123;//数字var flag=true;//布尔//nullvar x;//undefined,未赋值//typeof()用于查看变量类型alert(str+".this is a "+typeof(str));alert(num+".this is a "+typeof(num));alert(flag+".this is a "+typeof(flag));

在JS中模拟JAVA的函数重载功能:

//JS中不存在函数重载,但是可以模拟出函数重载的功能//定义不指定参数的函数,通过判断函数具有的参数个数,来控制函数返回不同值function add(){if(arguments.length==2)return arguments[0]+arguments[1];else if(arguments.length==3)return arguments[0]+arguments[1]+arguments[2];}var num1=add(1,2);var num2=add(1,2,3);alert(num1+" "+num2);



原创粉丝点击