zoukankan      html  css  js  c++  java
  • 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>
            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
  • 相关阅读:
    c++ primer 中讲的顶层const 和 底层 const 理解
    github 0 学习
    MySQL 0 学习
    c++11 move构造函数和move operator 函数 学习
    c++11 多线程 1
    c++ 多线程 0
    学习 emplace_back() 和 push_back 的区别 emplace_back效率高
    crontab执行脚本失败问题
    lucene 排序
    maven 内置变量
  • 原文地址:https://www.cnblogs.com/yaowen/p/6890723.html
Copyright © 2011-2022 走看看