前端八股-JavaScript篇之面向对象

对象创建的方式

5种方法

  1. 对象字面量:使用花括号 {} 定义对象并初始化其属性和方法。例如:

    const obj = {
      name: 'John',
      age: 30,
      sayHi: function() {
        console.log('Hi!');
      }
    };
    
  2. 构造函数:使用 new 关键字和构造函数创建一个新对象。例如:

    function Person(name, age) {
      this.name = name;
      this.age = age;
      this.sayHi = function() {
        console.log('Hi, my name is ' + this.name + ' and I am ' + this.age + ' years old.');
      };
    }
    
    const person = new Person('John', 30);
    
  3. 原型:使用构造函数的原型属性添加属性和方法。例如:

    function Person(name, age) {
      this.name = name;
      this.age = age;
    }
    
    Person.prototype.sayHi = function() {
      console.log('Hi, my name is ' + this.name + ' and I am ' + this.age + ' years old.');
    };
    
    const person = new Person('John', 30);
    
  4. Object.create():使用 Object.create() 方法创建一个新对象,该对象的原型指向传递的对象。例如:

    const person = Object.create({
      sayHi: function() {
        console.log('Hi!');
      }
    });
    
    person.name = 'John';
    person.age = 30;
    
  5. **class:**使用 class 关键字定义一个类并创建对象。例如:

    class Person {
      constructor(name, age) {
        this.name = name;
        this.age = age;
      }
      
      sayHi() {
        console.log('Hi, my name is ' + this.name + ' and I am ' + this.age + ' years old.');
      }
    }
    
    const person = new Person('John', 30);
    

#对象继承的方式

  1. 原型链继承:通过将子类的原型设置为父类的实例来实现继承。这样,子类就可以访问父类原型中的属性和方法。例如:Dog.prototype = new Animal();

    function Animal(name) {
      this.name = name;
    }
    
    Animal.prototype.eat = function() {
      console.log('I am eating');
    }
    
    function Dog(name) {
      this.name = name;
    }
    
    Dog.prototype = new Animal();
    
    const dog = new Dog('Tom');
    dog.eat(); // 'I am eating'
    
  2. 构造函数继承:通过在子类中调用父类构造函数来继承父类的属性和方法。例如:function Dog(name) { Animal.call(this, name);}

    function Animal(name) {
      this.name = name;
    }
    
    Animal.prototype.eat = function() {
      console.log('I am eating');
    }
    
    function Dog(name) {
      Animal.call(this, name);
    }
    
    const dog = new Dog('Tom');
    
  3. 组合继承:将原型链继承和构造函数继承结合起来使用,既继承了父类的属性和方法,也避免了原型链继承的缺点。例如:

    function Animal(name) {
      this.name = name;
    }
    
    Animal.prototype.eat = function() {
      console.log('I am eating');
    }
    
    function Dog(name) {
      Animal.call(this, name);
    }
    
    Dog.prototype = new Animal();
    Dog.prototype.constructor = Dog;
    
    const dog = new Dog('Tom');
    dog.eat(); // 'I am eating'
    
  4. ES6 的 class 继承:通过使用 extends 关键字和 super() 方法来继承父类的属性和方法。例如:

    class Animal {
      constructor(name) {
        this.name = name;
      }
      
      eat() {
        console.log('I am eating');
      }
    }
    
    class Dog extends Animal {
      constructor(name) {
        super(name);
      }
    }
    
    const dog = new Dog('Tom');
    dog.eat(); // 'I am eating'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蹦卡啦撒卡玛卡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值