美文网首页
前端(获取地址栏参数、Math、函数继承......)

前端(获取地址栏参数、Math、函数继承......)

作者: 埃菲尔上的铁塔梦i | 来源:发表于2018-08-26 16:08 被阅读0次

1、获取地址栏参数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取地址栏参数</title>
    <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>
</html>

2、Math

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
    <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>
</head>
<body>
    
</body>
</html>

3、单体创建对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>单体创建对象</title>
    <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>
</head>
<body>
    
</body>
</html>

4、工厂模式创建对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>工厂模式创建对象</title>
    <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>
</head>
<body>
    
</body>
</html>

5、构造函数

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>构造函数</title>
   <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>
</head>
<body>
   
</body>
</html>

6、原型模式

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>原型模式</title>
   <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>
</head>
<body>
   
</body>
</html>

7、call和apply

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>call和apply</title>
    <script type="text/javascript">
        /*
        call和apply的区别

        二者都可以改变当前的this,区别在于apply方法要将参数放入数组中再传参
        */
        function aa(a,b){
            alert('我的this是' + this + ',我的a是' + a + ',我的b是' + b);
        }

        //我的this是[object Window],我的a是2,我的b是3
        // aa(2,3);

        //我的this是abc,我的a是2,我的b是3
        // aa.call('abc',2,3);

        //我的this是abc,我的a是2,我的b是3
        aa.apply('abc', [2,3]);
    </script>
</head>
<body>
    
</body>
</html>

8、函数的继承

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>函数的继承</title>
   <script type="text/javascript">
       //父类
       function Fclass(name, age){
           this.name = name;
           this.age = age;
       }
       Fclass.prototype.showName = function(){
           alert(this.name);
       }
       Fclass.prototype.showAge = function(){
           alert(this.age);
       }

       //子类
       function Sclass(name, age, job){
           //属性用call或者apply的方式来继承
           Fclass.call(this, name, age);
           this.job = job;
       }
       //方法继承:将父类的一个实例赋值给子类的原型属性
       Sclass.prototype = new Fclass();
       Sclass.prototype.showJob = function(){
           alert(this.job);
       }

       //由于已经继承了父类的属性和方法,所以可以直接调用
       var Driver = new Sclass('tom',18,'老司机');
       Driver.showName();
       Driver.showAge();
       Driver.showJob();
   </script>
</head>
<body>
   
</body>
</html>

9、新增选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增选择器</title>
    <script type="text/javascript">
        window.onload = function(){
            var oDiv = document.querySelector('#div1');
            alert(oDiv);//弹出[object HTMLDivElement],表示选择了该Div

            //如果要选择多个元素用querySelectorAll
            var aLi = document.querySelectorAll('.list li');
            alert(aLi.length);//8
        }
    </script>
</head>
<body>
    <div id="div1">这是一个div元素</div>
    <ul class="list">
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
    </ul>
</body>
</html>

10、jQuery加载

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery加载</title>
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        // alert($);//弹出function (a,b){return new n.fn.init(a,b)}表示JQuery已经引进来了,这是它的一个构造函数

        //JS写法
        window.onload = function(){
            var oDiv = document.getElementById('div');
            alert(oDiv.innerHTML);//这是一个div元素
        }

        //jQuery的完整写法
        //比上面JS写法先弹出,因为window.onload是把页面元素加载、渲染完才弹出,而ready()是把所有页面的节点加载完之后就弹出了,不用等渲染了
        /*$(document).ready(function(){
            var $div = $('#div');
            alert('jQuery:' + $div.html());//jQuery:这是一个div元素
        })*/

        //简写方式
        $(function(){
            var $div = $('#div');//CSS样式怎么写,这里就怎么写
            //html()方法相当于原生JS的innerHTML
            alert($div.html() + 'jQuery');
        })
    </script>
</head>
<body>
    <div id="div">这是一个div元素</div>
</body>
</html>

相关文章

  • 面向对象

    获取地址栏参数 math 单体创建对象 工厂模式创建对象 构造函数 原型模式 call和apply 函数的继承 新...

  • 前端(十五)

    获取地址栏参数 Math 单体创建对象 工厂模式创建对象 构造函数 原型模式 call和apply 函数的继承 新...

  • 前端(获取地址栏参数、Math、函数继承......)

    1、获取地址栏参数 2、Math 3、单体创建对象 4、工厂模式创建对象 5、构造函数 6、原型模式 7、call...

  • 面向对象、继承

    (1)获取地址栏参数 (2)Math (3)单体创建对象 (4)构造函数 (5)工厂模式创建对象 (6)原型模式 ...

  • 面向对象 继承

    (1)获取地址栏参数 (2)Math (3)单体创建对象 (4)构造函数 (5)工厂模式创建对象 (6)原型模式 ...

  • js面向对象、继承

    1、获取地址栏参数 2、math 3、单体创建对象 4、工厂模式创建对象 5、构造函数 6、原型模式 7、call...

  • JavaScript学习四

    封闭函数 用变量的方式定义函数 闭包 闭包存循环的索引值 闭包做选项卡 获取地址栏参数 Math 单体创建对象 工...

  • 前端(十五)

    1.获取地址栏参数 2.Math 3.单体创建对象 4.工厂模式创建对象 5.构造函数 6.原型模式 7.函数的继...

  • 获取地址栏参数,Math

    1、Math

  • vue工具类之——URL地址栏参数操作

    地址栏参数的获取,返回object地址栏参数的删除,返回string地址栏参数的修改,返回string /** *...

网友评论

      本文标题:前端(获取地址栏参数、Math、函数继承......)

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