JavaScript基础语法
2.1前言
1、变量的定义;
2、程序的结构;
3、数组操作;
4、函数的定义及使用;
2.2JJavaScript变量
在Java如果要定义变量语法:“数据类型 变量 = 内容”。对于数据类型,还有许多属于自定义的类型、JavaScript很简单,可以不声明变量类型,直接编写变量,变量的的类型是根据设置的内容来决定的。
范例:观察程序代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript</title>
<script type="text/javascript" src="../js/testjs.js">
</script>
</head>
<body>
</body>
</html>
temp = 10; //temp为整形
alert(temp + temp);
temp = 10.2; //temp为浮点型
alert(temp + temp);
temp = "2.1.1"; //temp为字符串
alert(temp + temp);
temp = "30.1.1"; //temp为字符串
temp = 10; //修改类型
alert(temp + temp);
整个JavaScript操作语法之中,并没有那么严格的语法要求。因为从我们正常来讲应该是美工学的,不是程序开发人员学习的。使用他最初的设计逻辑没有那么强。
如果有需要也可以强制性的使用var来定义变量。
var temp = 30; //temp为字符串
temp = "10"; //修改类型
alert(temp + temp);
JavaScript没有必要怎么的概念的复杂,一般情况下,在JavaScript定义外部变量是使用var,也可以不使用。
但是既然说到字符串的事情,也就产生了新的事情。
范例:字符串比较
var temp = "hello" ;
alert(temp == "hello");
范例:判断字符串为空
var temp = "hello" ;
alert(temp != "");
范例:字符串替换
var temp = "hello" ;
alert(temp.substring(1,3));//取得el
大部分的习惯在JavaScript里面都是存在的。
2.3程序逻辑
程序逻辑就是if、else、switch、while、for循环。
范例:if
var temp = "hello" ;
if (temp == "hhh"){
document.write("<h1>欢迎光临!</h1>");
}else{
document.write("<h1>滚蛋吧!</h1>");
}
范例:for循环
var temp = "hello";
for(x = 0 ; x < 100 ; x ++){
temp+=x; //在后面接上
document.write("<h1>"+temp+"</h1>");//可以输出的正确表达
}
**范例:**打印乘法口诀表
document.write("<table border='1'>")//写一个表格
var temp = 1;
for (i = 1 ; i < 10 ; i ++){
document.write("<tr>");//表格换行
for (j = 1 ; j <= i; j ++){
temp = i*j;
document.write("<td>"+j+"*"+i+"="+(i * j)+"</td>");//可以输出的正确表达
//<td>一行内输出</td>
}
for (j = 1 ; j < 10 - i ; j ++){
document.write("<td> </td>")
}
document.write("</tr>");
}
document.write("</table>")
另外要告诉一点,以后如果想要进行代码的错误调试,可以通过如下语句完成:
console.log("一些信息");
以后会使用这种代码做一些我们后台的检测观察。
2.4数组
数组是一个类,使用的是Array类描述数组概念,可以使用length取得数组长度,也可以使用
范例:
var data = new Array(3); //3个元素的内容
data[0] = 10;
data[1] = 20;
data[2] = 30;
data[10] = 90;
document.write("<h1>数组长度:"+data.length+"</h1>")
for (x = 0; x < data.length ; x ++){
document.write("<h1>"+data[x]+"</h1>");
}
Jav中数组的设计是很牛的一个思想,可以随意动态扩充数组大小。但是如果没有的内容不会有0\null来描述,使用“undefined”来描述
2.5函数(function)
如果使用Java写函数
[public | protectd | private] [static] [abstratct] [final] [native] [synchronized] 方法返回值 方法名称 (参数类型 变量,… | 参数类型 …变量)[throws 异常,异常,…]{
//方法代码
[return [返回值];]
}
那么在JavaScript中函数的定义:
function 函数名称(参数){
[return 返回值 ;]
}
在定义函数时不需要考虑返回值类型,如果要返回那么久直接编写return。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript</title>
<script type="text/javascript" src="../js/testjs.js">
</script>
</head>
<body>
</body>
</html>
最厉害的是在条用函数的时候函数的参数个数可以随意写:
document.write(add(10,90,80,60,30));
在使用函数时如果函数的参数值不足,会自动使用“undefined”进行表示,肯定无法进行准确的计算,
如果在参数不足的期刊下已让要保障计算的文章,那么久使用“函数名称.agruments”进行判断,此操作我们表示的是函数的参数的内容,以数组的形式操作。使用length可以判断长度。
范例:改善调用
function add(x,y) {
if (add.arguments.length == 2) {
return x+y;
}else if (add.arguments.length == 1){
return x;
}else {
return "滚蛋吧!";
}
}
alert(add(10,90));
document.write("<h1>");
document.write(add(10,90,56));
document.write("</h1>");
需要注意的是在JavaScript里面也是有面向对象的编程。都是以函数为基础的。
范例:面向对象
function Person(name,age){
this.name = name ;
this.age = age ;
this.toString= function(){ //表示为我们类中扩充方法
return "姓名:"+this.name+",年龄"+this.age;
}
}
document.write("<h1>");
document.write(new Person("张三",20).toString());
document.write("</h1>");
如果我们在代码中写的是“function(){}”的操作,那么就表示匿名函数。
2.6总结
1、JJavaScript的语法完全考虑到美工的不理智性而设计的;
2、JavaScript语法都是以短小精悍为主;
3、JavaScript原本就不是面向对象的编程语言,支持的面向对象,只是一个娱乐;
完整代码文件可以查看我的资源。
学习快乐!!!