首页 > 代码库 > js35
js35
<!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>
js35
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。