创建对象的方式
构造器方式
<%@ 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> </body> <script type="text/javascript"> function Person(name,age){ this.name=name; this.age=age; this.eat=function(){ alert("eat"+name+age); } } var person=new Person("ton",1); person.eat(); </script> </html>
json方式
<%@ 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> </body> <script type="text/javascript"> var person={ name:"tom", age:21, eat:function(){ alert("ok"+person.age) } } alert(person.name) alert(person.age) person.eat(); </script> </html>