js高级笔记

学习笔记

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .dv {
            width: 400px;
            height: 400px;
            background: pink;
        }

        .map {
            width: 800px;
            height: 600px;
            background: #ccc;
            position: relative;
        }
    </style>
</head>
<body>

<div class="map"></div>

<input type="button" value="改变" id="btn"/>

<div class="dv" id="dv"></div>

<script>
    //        三种创建对象方式
    //字面量方式
    var per = {
        name: 'liu',
        sex: '男',
        age: '18',
        eat: function () {
            console.log('吃饭啊');
        }
    }

    //调用系统方式生成
    var persen = new Object()
    persen.name = 'liuliang'
    persen.age = '18'
    persen.sex = '女'
    persen.eat = function () {
        console.log('我要吃饭啦~~ ');
    }

    //自定义常见对象方式
    function Person(name, sex, age) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.eat = function () {
            console.log('我爱吃饭饭');
        };
    }

    //        工厂模式创建对象
    function createObject(name, sex, age) {
        var obj = new Object();
        obj.name = name;
        obj.sex = sex;
        obj.age = age;
        obj.eat = function () {
            console.log('吃饭吃饭~~')
        }
        return obj;
    }

    function CreateObject(name, sex, age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.eat = function () {
            console.log('吃吃吃~~~')
        }
    }

    //体会面向对象思想  案例  点击按钮改变div大小背景
    function ChangeStyle(btnObj, dvObj, json) {
        this.btnObj = btnObj;
        this.dvObj = dvObj;
        this.json = json;
    }

    ChangeStyle.prototype.init = function () {
        var that = this;
        this.btnObj.onclick = function () {
            for (var key in that.json) {
                that.dvObj.style[key] = that.json[key];
            }
        }
    }

    var json = {
        'width': '600px',
        'height': '600px',
        'background': 'green'
    };
    var change = new ChangeStyle(document.getElementById('btn'), document.getElementById("dv"), json);
    change.init();

    //利用原型共享数据
    function Student(name, age, sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    Student.prototype.eat = function () {
        console.log('吃饭');
    }
    Student.prototype.study = function () {
        console.log('做饭');
    }

    //    var stu = new Student('刘宣亮', '20', '男');
    //    console.dir(stu);
    //    console.dir(Student);

    //原型的简单语法
    function People(name, age, sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    People.prototype = {
        constructor: People,
        height: '80',
        width: '100',
        eat: function () {
            console.log('我要吃饭啊~~')
        },
        study: function () {
            console.log('我要学士')
        }
    };

    //    var ple = new People('段飞', 20, '男')
    //    ple.eat();
    //    ple.study();
    //    console.dir(People)
    //    console.dir(ple)

    //原型中的方法是可以相互访问的
    function Animal(name, age) {
        this.name = name;
        this.age = age;
    }

    //原型中添加方法
    Animal.prototype.eat = function () {
        console.log('我在吃饭');
        this.play();
    }
    Animal.prototype.play = function () {
        console.log('我在玩耍');
        this.sleep();
    }
    Animal.prototype.sleep = function () {
        console.log('我在睡觉')
    }

        //var dog = new Animal('龙', '18')
        //dog.eat();

        //贪吃蛇 地图  和食物
        //产生随机数对象
    (function (window) {
        function Random() {
        }

        Random.prototype.getRandom = function (min, max) {
            return Math.floor(Math.random() * (max - min) + min);
        }

        window.Random = new Random();
    }(window));

    //产生小方块对象
    (function (window) {

        var map = document.querySelector('.map');

        function Food(width, height, color) {
            this.width = width || 20;
            this.height = height || 20;
            this.color = color;
            this.x = 0;
            this.y = 0;
            this.element = document.createElement('div');
        }

        //初始化小方块的显示的效果及位置---显示地图上
        Food.prototype.init = function (map) {
            var div = this.element;

            div.style.position = 'absolute';
            div.style.width = this.width + 'px';
            div.style.height = this.height + 'px';
            div.style.backgroundColor = this.color;
            //小方块添加到map地图中
            map.appendChild(div);

            //产生随机位置
            this.render(map)
        };

        //产生随机位置
        Food.prototype.render = function (map) {
            var x = Random.getRandom(0, map.offsetWidth / this.width) * this.width;
            var y = Random.getRandom(0, map.offsetHeight / this.height) * this.height;
            this.x = x;
            this.y = y;
            var div = this.element;
            div.style.left = this.x + 'px';
            div.style.top = this.y + 'px';
        }

        var fd = new Food(20, 20, 'red');
        fd.init(map)

    }(window));

</script>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值