美文网首页
七、ES6的类、构造函数、原型链

七、ES6的类、构造函数、原型链

作者: 懒羊羊3号 | 来源:发表于2018-11-30 20:02 被阅读0次

1、目前在 JavaScript 中使用最广泛、认同度最高的一种创建自定义类型的方法

function Person(name, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.friends = ["ZhangSan", "LiSi"];
}

Person.prototype = {
    constructor : Person,
    sayName : function(){
        console.log(this.name);
    }
}

var person1 = new Person("Stone", 28, "Software Engineer");
var person2 = new Person("Sophie", 29, "English Teacher");

person1.friends.push("WangWu");
console.log(person1.friends);    // "ZhangSan,LiSi,WangWu"
console.log(person2.friends);    // "ZhangSan,LiSi"
console.log(person1.friends === person2.friends);    // false
console.log(person1.sayName === person2.sayName);    // true

2、new创造对象的方法等同于

//构造函数的原型(prototype)与实例的原型(_proto_)
function Person(){}
var person = new Person();
console.log(person.__proto__ === Person.prototype); // true
//另一种创造构造函数的方法
function Person(){}
var person = new Person(); 
// 上一行代码等同于以下过程 ==> 
var person = {};
person.__proto__ = Person.prototype;
Person.call(person); //以 person 为作用域执行了 Person 函数

3、ES6的类
//ES5写法

//构造函数
function Point(x, y) {
  this.x = x;
  this.y = y;
}
//在原型上定义一个toString方法
Point.prototype.toString = function () {
  return '(' + this.x + ', ' + this.y + ')';
};
//实例化
var p = new Point(1, 2);
p.x  //1
p.y //2
p.toString() //"(1+2)"

//ES6的语法糖

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
  toString() {
    return '(' + this.x + ', ' + this.y + ')';
  }
}

//全部定义在原型链上

class Point {
  constructor() {}
  toString() {}
  toValue() {}
}
// 等同于
Point.prototype = {
  constructor() {},
  toString() {},
  toValue() {},
};

js模式

1、工厂模式

function createPerson(name, age, job){
        var o = new Object();
        o.name = name;
        o.age = age;
        o.job = job;
        o.sayName = function(){
            alert(this.name);
        };
        return o;
 }

2、构造函数
方法私有

function Person(name, age, job){
        this.name = name;
        this.age = age;
        this.job = job;
        this.sayName = function(){
            alert(this.name);
        };
 }

3、原型
方法共享

对象字面量创造原型

constructor 属性不再指向 Person
constructor : Person,

function Person(){}
Person.prototype = {
    //特意加上
    constructor : Person,
    name : "Nicholas",
    age : 29,
    job: "Software Engineer",
    sayName : function () {
        alert(this.name);
    }
};

4、原型加构造函数组合

function Person(name, age, job){
    this.name = name; 
    this.age = age;
    this.job = job;
    this.friends = ["Shelby", "Court"];
  }
Person.prototype = {
    constructor : Person,
    sayName : function(){
        alert(this.name);
    }
}

5、类的一些特性
静态方法

class Foo {
  static classMethod() {
    return 'hello';
  }
}
Foo.classMethod() // 'hello'
var foo = new Foo();
foo.classMethod() // TypeError: foo.classMethod is not a function
// 可以被子类继承
class Bar extends Foo {}
Bar.classMethod() // 'hello'

6、类的继承

class ColorPoint extends Point {
  constructor(x, y, color) {
    super(x, y); // 调用父类的constructor(x, y)
    this.color = color;
  }
  toString() {
    return this.color + ' ' + super.toString(); // 调用父类的toString()
  }
}

tips: react在除了constructor之外的生命周期已经传入了this.props了,完全不受super(props)的影响。

相关文章

  • ES5里面的对象和继承

    构造函数和原型链 注意:原型链上面的属性会被多个实例共享,而构造函数不会 web类继承Student类 原型链 ...

  • TypeScript进阶(类)

    类 传统方法中,JavaScript 通过构造函数实现类的概念,通过原型链实现继承。而在 ES6 中,我们终于迎来...

  • js6种继承方式

    首先我们要提供一个父类 原型链继承: 原型链可继承的属性有:实例的构造函数的属性,父类构造函数属性,父类原型的属性...

  • 面向对象继承的方式

    创建父类 原型链继承:将父类的实例作为子类的原型 借用构造函数继承:在子类型构造函数的内部调用父类的构造函数 组合...

  • js 继承的三种方式构造函数、原型链、组合继承

    第一种方式:js使用构造函数的继承。 缺点:无法继承父类的原型链。 // 构造函数继承 缺点:没有继承原型链 fu...

  • js集成

    原始继承模式--原型链 2:借用构造函数 3:共享构造原型 4:圣杯模式原型链; 构造函数; 共享原型; 圣杯模式...

  • Typescript中的类

    在ES5中, 我们通过构造函数实现类的概念, 通过原型链实现集成。在ES6中,引入了class, Typescri...

  • 七、ES6的类、构造函数、原型链

    1、目前在 JavaScript 中使用最广泛、认同度最高的一种创建自定义类型的方法 2、new创造对象的方法等同...

  • js 集成模式 07-24

    **原始继承模式--原型链 2:借用构造函数 3:共享构造原型 4:圣杯模式**一:原型链; 二:构造函数; 三:...

  • 四.JavaScript——构造函数constructor、ne

    构造函数constructor Java/c++ 基于类 JavaScript 基于原型 es6 引入的 clas...

网友评论

      本文标题:七、ES6的类、构造函数、原型链

      本文链接:https://www.haomeiwen.com/subject/vvckcqtx.html