es6--对象和class

一、创建对象的方式

原先

    <script>
        //工厂函数
        function createObj(){
            var obj = {}
            obj.name = '蒸羊羔',
            obj.material = []
            return obj
        }
        var obj1 = createObj()
        console.log(obj1);
        var obj2 = createObj()
        console.log(obj2);
    </script>

现在

    <script>
        //工厂函数
        function createObj(name){
            var obj = {}
            obj.name = name,
            obj.material = []
            return obj
        }
        var obj1 = createObj("甲")
        console.log(obj1);
        var obj2 = createObj("乙")
        console.log(obj2);
    </script>

请添加图片描述
自定义函数

    <script>
        //工厂函数
        function createObj(name){
            //自动对象
            this.name = name
            this.material = []
            this.cook = function(){

            }
            //自动返回
        }
        var obj1 = new createObj("甲")
        console.log(obj1)
    </script>

请添加图片描述

二、构造函数注意问题

    <script>
        //1.首字母大写
        function CreateObj(name){
            this.name = name
        }
        var obj1 = new CreateObj("周六")
        console.log(obj1)
    </script>

请添加图片描述
2.构造函数不写return

    <script>
        //1.构造函数能当成普通函数用
        function CreateObj(name){
            this.name = name
        }
        var obj1 = CreateObj("周六")
        console.log(obj1,window.name)
    </script>

请添加图片描述

    <script>
        //this指向
        function CreateObj(name){
            console.log(this)
            this.name = name

            this.cook = function(){
                console.log(this.name)
            }
        }
        var obj1 = new CreateObj("周六") //实例化过程,实例对象已经生成
        console.log(obj1)
        obj1.cook()
    </script>

请添加图片描述

三、面向对象的原型

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="box1">
        <h1></h1>
        <ul></ul>
    </div>
    <div class="box2">
        <h1></h1>
    </div>
    <script>
        var data = {
            title:"体育",
            list:["体育1","体育2","体育3"]
        }

        function CreateList(select,data){
            this.ele = document.querySelector(select)
            this.title = data.title,
            this.list = data.list

            // this.render = function(){
            //     //渲染页面
            //     var h1 = this.ele.querySelector("h1")
            //     var ul = this.ele.querySelector("ul")
            //     console.log(h1,ul)

            //     h1.innerHTML = this.title
            //     ul.innerHTML = this.list.map(item=>`<li>${item}</li>`).join('')
            // }
            //原型
            CreateList.prototype.render = function(){
                 //渲染页面
                 var h1 = this.ele.querySelector("h1")
                var ul = this.ele.querySelector("ul")
                console.log(h1,ul)

                h1.innerHTML = this.title
                ul.innerHTML = this.list.map(item=>`<li>${item}</li>`).join('')
            }
        }

        var obj1 = new CreateList(".box1",data)
        console.log(obj1)

        obj1.render()

        //对象.__proto__ ===构造函数.prototype
        console.log(obj1.__proto__ === CreateList.prototype)
    </script>
</body>
</html>

请添加图片描述

    <script>
        function CreateObj(name){
            this.name = name
        }

        CreateObj.prototype.getName = function(){
            console.log("1111",this.name)
        }

        var obj1 = new CreateObj("周日")
        obj1.getName()
    </script>

请添加图片描述

四、class

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // function CreateObj(name){
        //     this.name = name
        // }

        // CreateObj.prototype.say = function(){
        //     console.log(this.name,"hello")
        // }

        class CreateObj {
            //构造器函数
            constructor(name){
                this.name = name
            }

            say(){
                console.log(this.name,"hello")
            }
        }
        

        var obj = new CreateObj("周日")
        console.log(obj)
        obj.say()
    </script>
</body>
</html>

请添加图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值