对象的创建与继承

创建对象

工厂模式 => 构造函数模式 => 原型对象模式 => 构造函数模式+原型对象模式

  • 工厂模式
function createPerson(name,age) {
    var o = new Object
    o.name = name
    o.age = age
    return o
}
var p = createPerson('guo',18)
  • 构造函数模式
function Person(name,age) {
    this.name = name
    this.age = age
}
var p = new Person('guo',18)
  • 原型对象模式
function Person() {}
Person.prototype.name = 'guo'
Person.prototype.age = 18
var p = new Person
  • 构造函数模式+原型对象模式
    私有属性使用构造函数,公有属性使用原型对象模式

继承

原型链=>借用构造函数=>组合

  • 原型链
function Person(name) {
    this.name = name
}
Person.prototype.getName = function() {
    return this.name 
}
function Male() {
    this.sex = 'male'
}
Male.prototype = new Person('guo')
Male.prototype.getSex = function() {
    return this.sex
}
var m = new Male
  • 借用构造函数
function Person(name) {
    this.name = name
}
function Male() {
    Person.apply(this,arguments)
    this.sex = 'male'
}
var m = new Male('guo')
  • 组合
function Person(name) {
    this.name = name
}
Person.prototype.getName = function() {
    return this.name 
}
function Male() {
    Person.apply(this,arguments)//调用第二次Person
    this.sex = 'male'
}
Male.prototype = new Person()//调用第一次Person
Male.prototype.getSex = function() {
    return this.sex
}
var m = new Male('guo')
//而且原型对象上存在与实例重复的属性

Object.create()

原型式继承=>Object.create()

  • 原型式继承
function object(o) {
    function F() {}
    F.prototype = o
    return new F()
}
//一个对象作为另一个对象的基础,根据需求加以修改
var person = {
    name: 'guo',
    age: 18
}
var p = object(person)
p.name = 'fwq'
  • Object.create()
    在只有一个参数,Object.create()和object()行为相同

实际中使用继承

使用类实现继承

  • 不兼容Object.create()的情况下
function Person(name,age) {
    this.name = name
    this.age = age 
}
Person.prototype.getName = function() {
    return this.name
}
function Male() {
    Person.apply(this,arguments)
    this.sex = 'male'
}
//为了实现m.__proto__.__proto__ === Person.prototype
function F() {}
F.prototype = Person.prototype
Male.prototype = new F()
Male.prototype.getSex = function() {
    return this.sex
}
var m = new Male('guo',18)
//因为没有调用构造函数,只是使用Person.prototype的副本,所以Male.prototype上没有重复的属性

对比借用构造函数和原型链的组合继承模式

function Person(name,age) {
    this.name = name
    this.age = age 
}
Person.prototype.getName = function() {
    return this.name
}
function Male() {
    Person.apply(this,arguments)
    this.sex = 'male'
}
//为了实现m.__proto__.__proto__ === Person.prototype
Male.prototype = new Person()
Male.prototype.getSex = function() {
    return this.sex
}
var m2 = new Male('guo',18)
//因为是把Person的实例覆盖到Male.prototype上导致Male.prototype存在重复的属性

优点:只调用一次Person构造函数,而且在Male.prototype上没有重复的属性

  • 兼容Object.create()
    原来的代码
function F() {}
F.prototype = Person.prototype
Male.prototype = new F()

现在的代码

Male.prototype = Object.create(Person.prototype)

对比上面的例子,结果是一样的

function Person(name,age) {
    this.name = name
    this.age = age 
}
Person.prototype.getName = function() {
    return this.name
}
function Male() {
    Person.apply(this,arguments)
    this.sex = 'male'
}
//为了实现m.__proto__.__proto__ === Person.prototype
Male.prototype = Object.create(Person.prototype)
Male.prototype.getSex = function() {
    return this.sex
}
var m = new Male('guo',18)
//因为没有调用构造函数,只是使用Person.prototype的副本,所以Male.prototype上没有重复的属性

不使用类直接继承

  • 原型式继承
    一个对象作为另一个对象的基础,根据需求加以修改
var animal = {
    name: '动物'
}
var cat = Object.create(animal)
cat.say = function () {
    console.log('miao')
}
console.log(cat)

你可能感兴趣的:(对象的创建与继承)