• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

JavaScript对象和类的创建

武飞扬头像
悠然予夏
帮助1

1、面向过程与面向对象

  • 面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候再一个一个的依次调用就可以了。
  • 面向对象是把事务分解成为一个个对象,然后由对象之间分工与合作。

面向过程与面向对象对比

  面向过程 面向对象
优点 性能比面向对象高,适合跟硬件联系很紧密的东西,例如单片机就采用的面向过程编程。 易维护、易复用、易扩展,由于面向对象有封装、继承、多态性的特性,可以设计出低耦合的系统,使系统 更加灵活、更加易于维护
缺点 不易维护、不易复用、不易扩展 性能比面向过程低

2、对象与类

对象是由属性和方法组成的:是一个无序键值对的集合,指的是一个具体的事物

  • 属性:事物的特征,在对象中用属性来表示(常用名词)

  • 方法:事物的行为,在对象中用方法来表示(常用动词)

2.1、创建对象

  1.  
    //以下代码是对对象的复习
  2.  
    //字面量创建对象
  3.  
    var ldh = {
  4.  
    name: '刘德华',
  5.  
    age: 18
  6.  
    }
  7.  
    console.log(ldh);
  8.  
     
  9.  
    //构造函数创建对象
  10.  
    function Star(name, age) {
  11.  
    this.name = name;
  12.  
    this.age = age;
  13.  
    }
  14.  
    var ldh = new Star('刘德华', 18)//实例化对象
  15.  
    console.log(ldh);
学新通

如上两行代码运行结果为:

学新通

2.2、类

        在 ES6 中新增加了类的概念,可以使用 class 关键字声明一个类,之后以这个类来实例化对象。类抽象了对象的公共部分,它泛指某一大类(class)对象特指某一个,通过类实例化一个具体的对象  

(1)创建类

语法:

  1.  
    //步骤1 使用class关键字
  2.  
    class name {
  3.  
    // class body
  4.  
    }
  5.  
    //步骤2使用定义的类创建实例 注意new关键字
  6.  
    var xx = new name();

示例:

  1.  
    // 1. 创建类 class 创建一个 明星类
  2.  
    class Star {
  3.  
    // 类的共有属性放到 constructor 里面
  4.  
    constructor(name, age) {
  5.  
    this.name = name;
  6.  
    this.age = age;
  7.  
    }
  8.  
    }
  9.  
    // 2. 利用类创建对象 new
  10.  
    var ldh = new Star('刘德华', 18);
  11.  
    console.log(ldh);

以上代码运行结果:

学新通

通过结果我们可以看出,运行结果和使用构造函数方式一样  

(2)类 constructor 构造函数

        constructor() 方法是类的构造函数(默认方法),用于传递参数,返回实例对象,通过 new 命令生成对象实例时,自动调用该方法。如果没有显示定义, 类内部会自动给我们创建一个constructor()

  1.  
    class Person {
  2.  
    constructor(name,age) { // constructor 构造方法或者构造函数
  3.  
    this.name = name;
  4.  
    this.age = age;
  5.  
    }
  6.  
    }

(3)类创建添加属性和方法

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
     
  4.  
    <head>
  5.  
    <meta charset="UTF-8">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8.  
    <title>Document</title>
  9.  
    </head>
  10.  
     
  11.  
    <body>
  12.  
    <script>
  13.  
    // 1. 创建类 class 创建一个 明星类
  14.  
    class Star {
  15.  
    // 类的共有属性放到 constructor 里面
  16.  
    constructor(uname, age) {
  17.  
    this.uname = uname;
  18.  
    this.age = age;
  19.  
    }
  20.  
    sing(song) {
  21.  
    // console.log('我唱歌');
  22.  
    console.log(this.uname song);
  23.  
     
  24.  
    }
  25.  
    }
  26.  
     
  27.  
    // 2. 利用类创建对象 new
  28.  
    var ldh = new Star('刘德华', 18);
  29.  
    var zxy = new Star('张学友', 20);
  30.  
    console.log(ldh);
  31.  
    console.log(zxy);
  32.  
    // (1) 我们类里面所有的函数不需要写function
  33.  
    //(2) 多个函数方法之间不需要添加逗号分隔
  34.  
    ldh.sing('冰雨');
  35.  
    zxy.sing('李香兰');
  36.  
    </script>
  37.  
    </body>
  38.  
     
  39.  
    </html>
学新通

注意哟:

  1. 通过class 关键字创建类, 类名我们还是习惯性定义首字母大写

  2. 类里面有个constructor 函数,可以接受传递过来的参数,同时返回实例对象

  3. constructor 函数 只要 new 生成实例时,就会自动调用这个函数, 如果我们不写这个函数,类也会自动生成这个函数

  4. 多个函数方法之间不需要添加逗号分隔

  5. 生成实例 new 不能省略

  6. 语法规范, 创建类 类名后面不要加小括号,生成实例,类名后面加小括号, 构造函数不需要加function

(4)类的继承

  • 现实中的继承:子承父业,比如我们都继承了父亲的姓。
  • 程序中的继承:子类可以继承父类的一些属性和方法。

语法:

  1.  
    class Father{ // 父类
  2.  
    }
  3.  
    class Son extends Father { // 子类继承父类
  4.  
    }
  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
     
  4.  
    <head>
  5.  
    <meta charset="UTF-8">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8.  
    <title>Document</title>
  9.  
    </head>
  10.  
     
  11.  
    <body>
  12.  
    <script>
  13.  
    // 1. 类的继承
  14.  
    // class Father {
  15.  
    // constructor() {
  16.  
     
  17.  
    // }
  18.  
    // money() {
  19.  
    // console.log(100);
  20.  
     
  21.  
    // }
  22.  
    // }
  23.  
    // class Son extends Father {
  24.  
     
  25.  
    // }
  26.  
    // var son = new Son();
  27.  
    // son.money();
  28.  
    class Father {
  29.  
    constructor(x, y) {
  30.  
    this.x = x;
  31.  
    this.y = y;
  32.  
    }
  33.  
    sum() {
  34.  
    console.log(this.x this.y);
  35.  
     
  36.  
    }
  37.  
    }
  38.  
    class Son extends Father {
  39.  
    constructor(x, y) {
  40.  
    super(x, y); //调用了父类中的构造函数
  41.  
    }
  42.  
    }
  43.  
    var son = new Son(1, 2);
  44.  
    var son1 = new Son(11, 22);
  45.  
    son.sum();
  46.  
    son1.sum();
  47.  
    </script>
  48.  
    </body>
  49.  
     
  50.  
    </html>
学新通

(5)super 关键字

        super 关键字用于访问和调用对象父类上的函数。可以调用父类的构造函数,也可以调用父类的普通函数

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
     
  4.  
    <head>
  5.  
    <meta charset="UTF-8">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8.  
    <title>Document</title>
  9.  
    </head>
  10.  
     
  11.  
    <body>
  12.  
    <script>
  13.  
    // super 关键字调用父类普通函数
  14.  
    class Father {
  15.  
    say() {
  16.  
    return '我是爸爸';
  17.  
    }
  18.  
    }
  19.  
    class Son extends Father {
  20.  
    say() {
  21.  
    // console.log('我是儿子');
  22.  
    console.log(super.say() '的儿子');
  23.  
    // super.say() 就是调用父类中的普通函数 say()
  24.  
    }
  25.  
    }
  26.  
    var son = new Son();
  27.  
    son.say();
  28.  
    // 继承中的属性或者方法查找原则: 就近原则
  29.  
    // 1. 继承中,如果实例化子类输出一个方法,先看子类有没有这个方法,如果有就先执行子类的
  30.  
    // 2. 继承中,如果子类里面没有,就去查找父类有没有这个方法,如果有,就执行父类的这个方法(就近原则)
  31.  
    </script>
  32.  
    </body>
  33.  
     
  34.  
    </html>
学新通

注意:

  • 子类在构造函数中使用super, 必须放到 this 前面 (必须先调用父类的构造方法,在使用子类构造方法)
  • super关键字 用于访问和调用对象父类上的函数。可以调用父类的构造函数,也可以调用父类的普通函数。
  • 时刻注意this的指向问题,类里面的共有的属性和方法一定要加this使用
    • constructor中的this指向的是new出来的实例对象

    • 自定义的方法,一般也指向的new出来的实例对象

    • 绑定事件之后this指向的就是触发事件的事件源

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
     
  4.  
    <head>
  5.  
    <meta charset="UTF-8">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8.  
    <title>Document</title>
  9.  
    </head>
  10.  
     
  11.  
    <body>
  12.  
    <button>点击</button>
  13.  
    <script>
  14.  
    var that;
  15.  
    var _that;
  16.  
    class Star {
  17.  
    constructor(uname, age) {
  18.  
    // constructor 里面的this 指向的是 创建的实例对象
  19.  
    that = this;
  20.  
    console.log(this);
  21.  
     
  22.  
    this.uname = uname;
  23.  
    this.age = age;
  24.  
    // this.sing();
  25.  
    this.btn = document.querySelector('button');
  26.  
    this.btn.onclick = this.sing;
  27.  
    }
  28.  
    sing() {
  29.  
    // 这个sing方法里面的this 指向的是 btn 这个按钮,因为这个按钮调用了这个函数
  30.  
    console.log(this);
  31.  
     
  32.  
    console.log(that.uname); // that里面存储的是constructor里面的this
  33.  
    }
  34.  
    dance() {
  35.  
    // 这个dance里面的this 指向的是实例对象 ldh 因为ldh 调用了这个函数
  36.  
    _that = this;
  37.  
    console.log(this);
  38.  
     
  39.  
    }
  40.  
    }
  41.  
     
  42.  
    var ldh = new Star('刘德华');
  43.  
    console.log(that === ldh);
  44.  
    ldh.dance();
  45.  
    console.log(_that === ldh);
  46.  
     
  47.  
    // 1. 在 ES6 中类没有变量提升,所以必须先定义类,才能通过类实例化对象
  48.  
     
  49.  
    // 2. 类里面的共有的属性和方法一定要加this使用.
  50.  
    </script>
  51.  
    </body>
  52.  
     
  53.  
    </html
学新通
  • 在 ES6 中类没有变量提升,所以必须先定义类,才能通过类实例化对象
  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
     
  4.  
    <head>
  5.  
    <meta charset="UTF-8">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8.  
    <title>Document</title>
  9.  
    </head>
  10.  
     
  11.  
    <body>
  12.  
    <script>
  13.  
    // 父类有加法方法
  14.  
    class Father {
  15.  
    constructor(x, y) {
  16.  
    this.x = x;
  17.  
    this.y = y;
  18.  
    }
  19.  
    sum() {
  20.  
    console.log(this.x this.y);
  21.  
    }
  22.  
    }
  23.  
    // 子类继承父类加法方法 同时 扩展减法方法
  24.  
    class Son extends Father {
  25.  
    constructor(x, y) {
  26.  
    // 利用super 调用父类的构造函数
  27.  
    // super 必须在子类this之前调用
  28.  
    super(x, y);
  29.  
    this.x = x;
  30.  
    this.y = y;
  31.  
     
  32.  
    }
  33.  
    subtract() {
  34.  
    console.log(this.x - this.y);
  35.  
     
  36.  
    }
  37.  
    }
  38.  
    var son = new Son(5, 3);
  39.  
    son.subtract();
  40.  
    son.sum();
  41.  
    </script>
  42.  
    </body>
  43.  
     
  44.  
    </html>
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhghabge
系列文章
更多 icon
同类精品
更多 icon
继续加载