首页 > Web开发 > 详细

js35

时间:2017-05-22 18:54:23      阅读:1535      评论:0      收藏:0      [点我收藏+]
<!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

原文:http://www.cnblogs.com/yaowen/p/6890723.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!