【JS】什么是对象

<body>
    1.对象概念?
            Object-对象

    2.如何创建对象?
            let obj={
                属性:属性值,   --键值对(逗号隔开)
                属性2:属性值2
            }

<script>
    /*字面量方式创建*/
    let student={
        //属性(名词)
        name:"月月",
        className:"3",
        shenGao:"160",
        //行为(动词)
        eating:function () {
            console.log("吃饭")
        }
    }

    /*new关键词创建*/
    let obj=new Object();
        obj.name="月月";
        obj.age="24";
        obj.run=function () {
            console.log("跑步")
        }

    obj.run();

</script>

    3.对象中最重要的思想(面向对象)
        面向过程:强调的是事件的流程和顺序
        面向对象:强调的是角色、主体


    4.对象的特性
        封装、继承、多态、抽象(封装和抽象是现阶段需要掌握的)

        封装:将某个方法写成多用的
                getNode("div")

                function getNode(Nodes){

                    return document.getElementById(Nodes);
                }

        抽象:关注它的某些属性,把它抽取出来;作为共同的特性进行处理

<script>
    /*模板  通过模板创建对象*/
    function Student(stuId,stuName,stuSex,stuAge) {
            //属性  this指向实例化对象
            this.stuId=stuId;
            this.stuName=stuName;
            this.stuSex=stuSex;
            this.stuAge=stuAge;
            this.study=function () {
                console.log("我爱学习代码!!!")
            }
    }
    let myStudent=new Student(1,"月月","女",18);
    console.log(myStudent.stuName)
    myStudent.study();
    let myStudent2=new Student(2,"博文","男",28);
    console.log(myStudent2)

    /*let student1={
        id:"001",
        name:"月月",
        sex:"女",
        age:"24",
        study:function () {
            console.log("我爱学习代码!!!")
        }
    }
    let student2={
        id:"002",
        name:"博文",
        sex:"男",
        age:"28",
        study:function () {
            console.log("我爱学习代码!!!")
        }
    }*/
    
    /*let myArr=new Array();
    function Array() {
        this.push=function () {

        };
        this.pop=function () {

        }
    }
    myArr.push();*/






    function BoyFriend(height,handsome,money) {
        this.height=height;
        this.handsome=handsome;
        this.money=money;
        this.maimaimia=function () {
            this.money=this.money-5000;
            console.log("我现在还有:"+this.money)
        }
    }
    // let bf= new BoyFriend(180,true,parseInt(Math.random()*50000+100))
    // console.log(bf);
    // bf.maimaimia();

    /*创建多个*/
    let bfArray=new Array();  //创建好了放数组里
    function creatBf() {
        let num=parseInt(Math.random()*10)
        if(num>5){
            handsome=true
        }else{
            handsome=false
        }
        let bf= new BoyFriend(num,handsome,parseInt(Math.random()*50000+100))
        //放到数组中
        bfArray.push(bf);
        console.log(bfArray)
    }
    // creatBf();
    //每隔3秒创建一个
    setInterval(creatBf,2000);

    
</script>

    保留小数:toFixed()方法,括号内写保留小数的位数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值