今天回顾了一下javascript的知识,复习了一下,做了点笔记
01.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
/*
function fun(name, age){
alert(name+"年龄"+age);
}
fun("张三", 23);
*/
var fun=new Function("name", "age", "alert(name + '年龄'+age)");
fun("张三", 24);
</script>
</body>
</html>
02.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
/*
function fun(name, age){
alert(name+"年龄"+age);
}
fun("张三", 23);
*/
var fun=new Function("name", "age", "alert(name + '年龄'+age)");
fun("张三", 24);
alert("fun方法对象的方法参数"+fun.length);
</script>
</body>
</html>
03.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
/*
function fun(name, age){
alert(name+"年龄"+age);
}
fun("张三", 23);
*/
var fun=new Function("name", "age", "alert(name + '年龄'+age)");
fun("张三", 24);
alert("fun方法对象的方法参数"+fun.length);
alert("显示源码"+fun.toString());
alert("显示源码"+fun.valueOf());
</script>
</body>
</html>
04.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
var a = 22;
function fun(){
alert(a);
}
func();
</script>
</body>
</html>
05.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
//var a = 22;
function fun(){
a=22;//全局变量
var b=33;//局部变量
}
fun();
alert(a);
</script>
</body>
</html>
06.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
function fun(){
var a =33;
function func2(){
return a;
}
return func2;
}
var result = fun();
alert("访问fun的私有局部变量a"+result());
</script>
</body>
</html>
07.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
var name="hello world";
var object = {
name:"my object",
getNamefun:function(){
return function(){
return this.name;
};
}
};
alert(object.name);
alert(object.getNamefun()());
</script>
</body>
</html>
08.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
//对象初始化器方式
var tom = {
name:"tom",
age:2,
function1:function(){
alert("我的姓名"+this.name+",我的年龄:"+this.age);
},
function2:function(){
alert("我现在在上学");
}
};
alert(tom.name);
alert(tom.age);
tom.function1();
tom.function2();
//----------------------------------------
//构造函数的方式
function Dao(name, age){
this.name = name;
this.age = age;
this.function1 = function(){
alert("我的姓名"+this.name+",我的年龄:"+this.age);
};
this.function2 = function(){
alert("我现在在上学");
};
}
var dao = new Dao("dao,", 1);
alert(dao.name);
alert(dao.age);
dao.function1();
dao.function2();
//--------------------------------------
</script>
</body>
</html>
09.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
function Action(){
this.object = "对象属性";
Action.prototype.object2 = "对象属性";
var private_Object = "私有属性";
}
Action.classObject = "类属性";
alert(Action.classObject);
var action = new Action();
alert(action.object);
alert(action.object2);
</script>
</body>
</html>
10.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
//javascript中自定义对象的方法定义
function Action(){
var privateFunc = function(){
alert("私有方法");
};
privateFunc();
this.function1 = function(){
alert("对象方法1");
};
Action.prototype.function2 = function(){
alert("对象方法2");
};
}
Action.classFunction = function(){
alert("类方法");
};
Action.classFunction();
var action = new Action();
action.function1();
action.function2();
</script>
</body>
</html>
11.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
//javascript实现继承特性(Apply()实现属性和方法的继承,单纯的继承属性和方法)
function Person(name, age){
this.name = name;
this.age = age;
this.function1 = function(){
alert("我的姓名"+this.name+",我的年龄:"+this.age);
};
this.function2 = function(){
alert("我现在在上学");
};
}
function Student(name, age){
Person.apply(this, [name, age]);
}
var student = new Student("张三", 22);//student这个对象还是Student这个类产生的
alert(student.name);
alert(student.age);
student.function1();
student.function2();
</script>
</body>
</html>
12.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
function Person(name, age){
this.name = name;
this.age = age;
this.function1 = function(){
alert("我的姓名"+this.name+",我的年龄:"+this.age);
};
this.function2 = function(){
alert("我现在在上学");
};
}
function Student(name, age){
Person.apply(this, [name, age]);
}
Student.prototype=new Person();
var student = new Student("张三", 22);//student这个对象的原型是Person类(原因是Student.prototype=new Person();)
alert(student.name);
alert(student.age);
student.function1();
student.function2();
</script>
</body>
</html>
13.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
function Person(){
this.action=function(){
alert(" 我是Person");
};
}
function Student(){
this.action=function(){
alert("我是Student");
};
}
Student.prototype=new Person();
function Teacher(){
this.action=function(){
alert("我是Teacher");
};
}
Teacher.prototype=new Person();
function fun(person){
if(person instanceof Person){
alert("true");
person.action();
}else{
alert("false");
}
}
var student = new student();
var teacher = new Teacher();
fun(student);
fun(teacher);
</script>
</body>
</html>
这些是javascript的重要使用方法