美文网首页
2018-12-08

2018-12-08

作者: 11bbc2c5d0c6 | 来源:发表于2018-12-08 13:24 被阅读0次
闭包做私有变量计数器
<script type="text/javascript">
    //闭包的用途:私有变量计数器
    var count = (function(){
        var a = 0;

        function bb(){
            a++;
            return a;
        }

        return bb;
    })();
    
    //每调用一次count,a就自增一次
    alert(count());//1
    alert(count());//2

    var c = count();
    alert(c);//3
</script>
闭包做选项卡
<style type="text/css">
    .btns{
        width: 500px;
        height: 50px;
    }
    /*选项卡的样式*/
    .btns input{
        width: 100px;
        height: 50px;
        background-color: #ddd;/*默认灰色*/
        color: #666;
        border: 0px;
    }
    /*被选中的选项卡的样式*/
    .btns input.cur{
        background-color: gold;
    }
    /*内容区的样式*/
    .contents div{
        width: 500px;
        height: 300px;
        background-color: gold;
        display: none;/*默认隐藏*/
        line-height: 300px;
        text-align: center;
    }
    /*被选中的内容区的样式*/
    .contents div.active{
        display: block;
    }
</style>
<script type="text/javascript">
    //闭包做选项卡
    window.onload = function(){
        var aBtn = document.getElementById('btns').getElementsByTagName('input');
        var aCon = document.getElementById('contents').getElementsByTagName('div');
        // alert(aCon.length);

        //循环所有的选项卡按钮
        for(var i=0; i<aBtn.length; i++){
            (function(i){
                //给每个选项卡按钮添加点击事件
                aBtn[i].onclick = function(){
                    //遍历所有选项卡按钮
                    for(var j=0; j<aBtn.length; j++){
                        //将每个选项卡按钮都设为灰色
                        aBtn[j].className = '';
                        //将每个内容区都隐藏
                        aCon[j].className = '';
                    }
                    //this代表当前点击的Button对象
                    this.className = 'cur';//当前点击的按钮为金色

                    // alert(i);//不加闭包时,不管点哪个按钮,i都等于3

                    //加闭包保存了索引值才有效
                    aCon[i].className = 'active';//当前点击的按钮对应的内容显示
                }
            })(i);
        }
    }
</script>
</head>
<body>
<div class="btns" id="btns">
    <input type="button" value="tab01" class="cur">
    <input type="button" value="tab02">
    <input type="button" value="tab03">
</div>
<div class="contents" id="contents">
    <div class="active">tab文字内容一</div>
    <div>tab文字内容二</div>
    <div>tab文字内容三</div>
</div>
</body>
跳转的源页面
    <script type="text/javascript">
        //存储跳转的源页面
        varUrl = document.referrer;
        //登录逻辑

        //跳转
        window.location.href = backUrl;
    </script>
获取地址栏参数
<script type="text/javascript">

    window.onload = function(){
        //url?aa=tom#12
        var data = window.location.search;//?aa=tom
        var hash = window.location.hash;//#12
        alert(hash);//#12

        var oSpan = document.getElementById('span01');
        // alert(data);//?aa=tom

        var arr = data.split('=');
        // alert(arr);//?aa,tom

        var name = arr[1];
        oSpan.innerHTML = name;
    }
</script>
</head>
<body>
<div>欢迎<span id="span01"></span>访问我的主页</div>
</body>
Math
<script type="text/javascript">
    // var num = Math.random();
    // alert(num);//弹出0-1之间的随机数

    var a = 10;
    var b = 20;
    // var num = Math.random()*(b-a)+a;
    // alert(num);//弹出10-20之间的随机数

    var arr = [];
    for(var i=0; i<20; i++){
        // var num = Math.floor(Math.random()*(b-a)+a);//向下取整,10-19
        var num = Math.floor(Math.random()*(b-a + 1)+a);//向下取整,10-20
        
        arr.push(num);//生成一个数就放进数组
    }
    alert(arr);//17,20,20,11,11,19,17,16,10,11,16,11,18,13,13,11,17,14,19,19
</script>
单体创建对象
<script type="text/javascript">
    var Tom = {
        // 属性
        name:'tom',
        age:18,

        // 方法
        showName:function(){
            alert(this.name);
        },
        showAge:function(){
            alert(this.age);
        }
    }

    //调用属性
    alert(Tom.name);
    alert(Tom.age);
    
    //调用方法
    Tom.showName();
</script>
工厂模式创建对象
<script type="text/javascript">
    function Person(name,age,job){
        //创建一个空对象
        // var o = new Object();//方式一
        var o = {};//方式二

        o.name = name;
        o.age = age;
        o.job = job;

        o.showName = function(){
            alert(this.name);
        }
        o.showAge = function(){
            alert(this.age);
        }
        o.showJob = function(){
            alert(this.job);
        }

        return o;
    }

    var Tom = Person('tom',18,'程序猿');
    Tom.showJob();

    var Jack = Person('jack',19,'攻城狮');
    Jack.showJob();
</script>
构造函数
<script type="text/javascript">
    function Person(name,age,job){
        this.name = name;
        this.age = age;
        this.job = job;

        this.showName = function(){
            alert(this.name);
        }
        this.showAge = function(){
            alert(this.age);
        }
        this.showJob = function(){
            alert(this.job);
        }
    }

    //new的作用就相当于工厂模式中最开始创建了一个空对象,最后把对象返回
    var Bob = new Person('bob',18,'产品汪');
    Bob.showJob();

    var Alex = new Person('alex',19,'运营喵');
    Alex.showJob();

    alert(Bob.showName == Alex.showName);//false
</script>
原型模式
<script type="text/javascript">
    function Person(name,age,job){
        this.name = name;
        this.age = age;
        this.job = job;

        Person.prototype.showName = function(){
            alert(this.name);
        }
        Person.prototype.showAge = function(){
            alert(this.age);
        }
        Person.prototype.showJob = function(){
            alert(this.job);
        }
    }

    //先去自己的对象中找showName函数,再去构造函数的原型找
    var Lucy = new Person('lucy',18,'测试鼠');
    //重写自身对象中的方法,不会影响其它对象
    Lucy.showName = function(){
        alert('我的名字是' + this.name);
    }
    Lucy.showName();//我的名字是lucy

    var Lily = new Person('lily',19,'市场鸡');
    Lily.showName();//lily

    alert(Lucy.showName == Lily.showName);//false
</script>

相关文章

网友评论

      本文标题:2018-12-08

      本文链接:https://www.haomeiwen.com/subject/iblqhqtx.html