js的数组、对象数组和对象的定义和js内部对象和with、for..in的用法

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'js2.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
//with是锁定对象
with(document)
{
writeln("<ol>");
writeln("<li>");
write("曹欢");
write("</li>");
write("<li>");
write("曹娟");
write("</li>");
write("</ol>");

writeln("<ul>");
writeln("<li>");
write("曹欢");
write("</li>");
write("<li>");
write("曹娟");
write("</li>");
write("</ul>");

};
var i = 0
function large()
{
i++;
if(i%2==1)
{
document.getElementById("image").width = 200;

}
else
{
document.getElementById("image").width = 468;
}

}
//for..in的使用以及对象的定义方式的解释
function member(name, age)//相当于构造函数
{
this.name = name;
this.age = age;
}

function showproperties(obj, objstring)
{
for(var i in obj)
{
document.write(objstring + "." + i + " = " + obj[i] + "<br/>");
}
}
var obj = new member("张三", 21);//建立对象,里面有两个属性(name, age)
showproperties(obj, "person");
//日期对象Date 月份是0-11,星期天使使星期七
/*var date = new Date();//如果构造函数没有传递参数,则可以写成new date不需要加后面的()
alert(date.getYear());
alert(date.getFullYear());
alert(date.getMonth()+1);
alert(date.getDate());
alert(date.getDay()+2);
document.write("现在时间是:"+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());*/
//数组,数组里存放的对象不一定要是同一种类型,数组默认的数据是undefined
var arr = new Array(3);
arr[0] = "曹欢";
var arr1 = new Array("曹欢",1,false);
//alert(arr1[2]);
var arr2 = ["caohuan",1,false];
alert(arr2[2]);
alert(arr2.length);
//一些练习
for(var i = 0; i < 10; i++)
{
//document.write(i+"<br/>");
}s
</script>
</head>

<body>
<!-- <img id="image" src="image/a.jpg.jpg"></img> <input type="button" onclick="large()" value="点击有惊喜啊!"> -->
</body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'js3.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
//一维数组的一些方法length join toString reverse valueOf push(添加数据) pop(取出数据)
var arr = ["曹欢","曹娟","爸爸",'妈妈'];

/*document.writeln(arr.length+"<br>");
document.writeln(arr.join(":")+"<br>");//join(参数)将数组转换成字符串,参数表示用什么分开(分隔符)
document.write(arr.reverse()+"<br>");//reverse()使数组翻转,改变的是原数组。
document.write(arr+"<br>");
document.write(arr.valueOf());*/
arr.push("苹果");
//alert(arr.pop())移除最上面的那个元素,并返回那个元素
//alert(arr[0]);
//二维数组
var arr2 = new Array(3);
arr2[0] = new Array(3);

arr2[0].push("a1");
arr2[0].push("a2");
arr2[0].push("a3");

arr2[1] = new Array(3);
arr2[1][0] = "b1";
arr2[1][1] = "b2";
arr2[1][2] = "b3";

arr2[2] = new Array(3);
arr2[2][0] = "c1";
arr2[2][1] = "c2";
arr2[2][2] = "c3";


for(var i = 0; i < arr2.length; i++)
{

for(var j = 0; j < arr2[i].length; j++)
{

document.write(arr2[i][j]+"<br/>");
}
}
//关于字符串的一些处理,
var str = new String("I love you");
//alert(str);
var str1 = "真的吗?";
//alert(str1);
//一些常见的方法charAt indexOf lastIndexOf replace search substring

with(document)
{
write("----------------------------------------","<br/>");//注这里用逗号和加号的效果一样
write(str.length,"<br/>");//注意这里的length不要加()
write(str.toUpperCase()+"<br/>");
write(str.charAt(2)+"<br/>");//返回某个位置的字符
write(str.indexOf('ve'),"<br/>");//返回某个字符的位置,如果不包含就返回-1
write(str.lastIndexOf('ve'),"<br/>");
write(str.replace('I','我'),'<br/>');//代替的只是个副本,并不是str本身
write(str,'<br/>');
write(str.search('I'),"<br/>");//返回该正则表达式的第一次出现的位置
write(str.substring(0,3),"<br/>");

}
//一个简单的例子
function go()
{
var emailValue = document.getElementsByName('email')[0].value;

if(emailValue.indexOf('@')==-1)
{
alert("不合法的email");
return;
}
alert("正确");
}
</script>
</head>

<body>
<input type="text" value="asd" name="email"><input type="button" onclick="go()" value="提交">
</body>
</html>

你可能感兴趣的:(with)