日期:2014-05-16  浏览次数:20430 次

js创建对象 创建方法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
  <TITLE> js创建实体对象 </TITLE> 
<script type="text/javascript" src="jquery.js"></script> 
<script> 
//自定义对象:学生类 
var Student={Name:"xiaoming",Age:20}; 
//alert(Student.Name); 
//自定义对象:城市类 
var cities={ 
  beijing:{Name:"北京",towns:["海淀","大兴","通州","朝阳"]}, 
  tianjin:{Name:"天津",People:2000}, 
  shanghai:{Name:"上海",People:2000} 
}; 
var tosws = cities; 
//循环遍历对象 
for(t in tosws){ 
  alert(tosws[t].Name); 
} 
//自定义方法并调用 
var gogo= function(){ 
return{ 
  say:function(mess){ 
   alert("哭着说:"+mess); 
  }, 
  tell:function(mess){ 
   alert("告诉我说:"+mess); 
  }, 
  speak:function(mess){ 
   alert("对我说:"+mess); 
  } 
}; 
}(); 
//js定义方法模型:var funs=function(){return{say:function(mess){},spake:function(mess){},cry:function(){}};}(); 
//js定义方法 必须一()结尾不然出错 
var funs = function() 
{ 
var a,b,c=false; 
return{ 
  say:function(str){ 
   alert(str); 
  }, 
  speak:function(str){ 
   alert(str); 
  } 
}; 
}(); 
//var 弱类型对象不用java的实例化方法:Users u = new Users("xiaoqing","你好a !!"); 
//var MyFun=function(){return {fun1:function(prop){...},fun2:function(prop){...},fun3:function(prop){...}};}(); 
function test(){ 
  var u = new Users("xiaoqing","你好a !!"); 
  u.say(); 
  funs.say("nihao"); 
  var s = new Student("小明","20"); 
  s.cry("考试补给和"); 
  gogo.tell("大家好"); 
} 

//js简单方法对象 模型:var funName={fun1:function(prop){...},fun2:function(prop){...}} 
var shortFun ={ 
init:function(){alert("初始化");}, 
say:function(mess){alert("say:"+mess);}, 
speak:function(mess){alert("speak:"+mess);} 
} 
</script> 
</HEAD> 
<BODY> 
   
</BODY> 
</HTML> 

?