zoukankan      html  css  js  c++  java
  • 构造函数和class函数

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        
    </body>
    </html>
    <script>
    function People(name,age){
        this.name = name;
        this.age = age;
    }
    People.info = 'bb' ;//直接挂载在构造函数上,所以是静态属性
    People.prototype.say=function(){
        console.log('这是People实例属性')
    }
    let p1 = new People('王少',22);
    console.log(p1);
    console.log(p1.name);//通过new出来的实例,访问到的属性叫做实例属性;
    console.log(p1.age);
    p1.say();//这是实例的方法
    console.log(People.info)//静态属性:通过构造函数,直接访问的属性,叫做静态属性。
    </script>
     
    ————————————————————————————————————————————————————————————————
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        
    </body>
    </html>
    <script>
        //创建个动物类
    class Animal{
        //这是 类的构造器 构造器的作用:每当new这个类的时候,必然会先执行构造器的代码
        constructor(name,age){
            this.name=name;
            this.age=age;
        }
        static info = 'gender';//在class内部,通过static 修饰的属性叫做静态属性;

        jion(){
            console.log('这是Animal实例属性');
        }

        static jiao(){
            console.log('这是Animal静态方法');//在class内部,通过static 修饰的属性叫做静态方法;
        }
    }
    const dog = new Animal('大黄',2);
    console.log(dog);
    console.log(dog.name);
    console.log(dog.age);
    dog.jion();
    console.log(Animal.info);
    Animal.jiao();
    </script>
  • 相关阅读:
    微信小程序购物商城系统开发系列-工具篇
    CSS实现导航条Tab切换的三种方法
    最详细win7下手动搭建PHP环境:apache2.4.23+php7.0.11
    读书笔记:《HTML5开发手册》Web表单
    jQuery可拖拽3D万花筒旋转特效
    框架基础:关于ajax设计方案(三)---集成ajax上传技术
    框架基础:ajax设计方案(二)---集成轮询技术
    框架基础:ajax设计方案(一)---集成核心请求
    Apache+PHP+MySQL
    自学 PHP,如何不走弯路?
  • 原文地址:https://www.cnblogs.com/manban/p/12038016.html
Copyright © 2011-2022 走看看