<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type=text/javascript charset=utf-8>
///-------------------------------------------------------------------
function Person(age,name){//类的定义,还没有运行起来
this.name = name;
if(!this.checkAge(age)){//写可以这样写,运行是确定this
throw new Error("年龄必须在0到150之间");
}
this.age = age;
}
Person.prototype = {
checkAge:function(age){
if(age>0 && age < 150){
return true;
}else{
return false;
}
}
}
Person.prototype["getName"] = function(){
return this.name || "USPCAT.COM";
}
//--------------------------------------------------------------------
//用命名规范来区别私有和共有变量
function Person(name,age,email){
//定义私有变量
this._name;//私有
this._age;//私有
this.setName(name);
this.setAge(age);
this.email = email;//共有
}
Person.prototype = {
setName:function(name){
this._name = name;
},
setAge :function(age){
if(age>0 && age < 150){
this._age = age;
}else{
throw new Error("年龄必须在0到150之间");
}
}
}
//-------------------------------------------------------------------*/
function person(name,age,email,sex){
this.email = email;//公有 变量
//name,age私有变量
this.getName = function(){
return this.name;
}
this.getAge = function(){
return this.age;
}
this.setName = function(name){
this.name = name;//动态添加name属性,先set再get
}
this.setAge = function(age){
if(age>0 && age < 150){
this.age = age
}else{
throw new Error("年龄必须在0到150之间");
}
}
var _sex = "M";//这也是私有变量的编写方式
this.getSex = function(){
return _sex;
}
this.setSex = function(){
_sex = sex
}
this.init = function(){
this.setName(name);
this.setAge(age);
}
this.init();
}
var p = new person("JIM",-1,"www.USPCAT@126.COM")
</script>
</head>
<body>
</body>
</html>
var getXHR = function(){
alert(2);
getXHR = function(){
alert(1);
}
}
getXHR();//2
getXHR();//1
getXHR();//1
var getXHR = function(){
var a = 1;
getXHR = function(){
return 2;
}
return a;
}
alert(getXHR());//1
alert(getXHR());//2
alert(getXHR());//2