對(duì)象的創(chuàng)建與繼承

創(chuàng)建對(duì)象

工廠模式 => 構(gòu)造函數(shù)模式 => 原型對(duì)象模式 => 構(gòu)造函數(shù)模式+原型對(duì)象模式

  • 工廠模式
function createPerson(name,age) {
    var o = new Object
    o.name = name
    o.age = age
    return o
}
var p = createPerson('guo',18)
  • 構(gòu)造函數(shù)模式
function Person(name,age) {
    this.name = name
    this.age = age
}
var p = new Person('guo',18)
  • 原型對(duì)象模式
function Person() {}
Person.prototype.name = 'guo'
Person.prototype.age = 18
var p = new Person
  • 構(gòu)造函數(shù)模式+原型對(duì)象模式
    私有屬性使用構(gòu)造函數(shù),公有屬性使用原型對(duì)象模式

繼承

原型鏈=>借用構(gòu)造函數(shù)=>組合

  • 原型鏈
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
  • 借用構(gòu)造函數(shù)
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)//調(diào)用第二次Person
    this.sex = 'male'
}
Male.prototype = new Person()//調(diào)用第一次Person
Male.prototype.getSex = function() {
    return this.sex
}
var m = new Male('guo')
//而且原型對(duì)象上存在與實(shí)例重復(fù)的屬性

Object.create()

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

  • 原型式繼承
function object(o) {
    function F() {}
    F.prototype = o
    return new F()
}
//一個(gè)對(duì)象作為另一個(gè)對(duì)象的基礎(chǔ),根據(jù)需求加以修改
var person = {
    name: 'guo',
    age: 18
}
var p = object(person)
p.name = 'fwq'
  • Object.create()
    在只有一個(gè)參數(shù),Object.create()和object()行為相同

實(shí)際中使用繼承

使用類實(shí)現(xiàn)繼承

  • 不兼容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'
}
//為了實(shí)現(xiàn)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)
//因?yàn)闆]有調(diào)用構(gòu)造函數(shù),只是使用Person.prototype的副本,所以Male.prototype上沒有重復(fù)的屬性

對(duì)比借用構(gòu)造函數(shù)和原型鏈的組合繼承模式

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'
}
//為了實(shí)現(xiàn)m.__proto__.__proto__ === Person.prototype
Male.prototype = new Person()
Male.prototype.getSex = function() {
    return this.sex
}
var m2 = new Male('guo',18)
//因?yàn)槭前裀erson的實(shí)例覆蓋到Male.prototype上導(dǎo)致Male.prototype存在重復(fù)的屬性

優(yōu)點(diǎn):只調(diào)用一次Person構(gòu)造函數(shù),而且在Male.prototype上沒有重復(fù)的屬性

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

現(xiàn)在的代碼

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

對(duì)比上面的例子,結(jié)果是一樣的

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'
}
//為了實(shí)現(xiàn)m.__proto__.__proto__ === Person.prototype
Male.prototype = Object.create(Person.prototype)
Male.prototype.getSex = function() {
    return this.sex
}
var m = new Male('guo',18)
//因?yàn)闆]有調(diào)用構(gòu)造函數(shù),只是使用Person.prototype的副本,所以Male.prototype上沒有重復(fù)的屬性

不使用類直接繼承

  • 原型式繼承
    一個(gè)對(duì)象作為另一個(gè)對(duì)象的基礎(chǔ),根據(jù)需求加以修改
var animal = {
    name: '動(dòng)物'
}
var cat = Object.create(animal)
cat.say = function () {
    console.log('miao')
}
console.log(cat)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 繼承有什么作用? (難度:3*) 繼承可以使一個(gè)對(duì)象直接使用另一個(gè)對(duì)象的屬性和方法。 有幾種常見創(chuàng)建對(duì)象的方式? ...
    coolheadedY閱讀 607評(píng)論 0 0
  • 問答 繼承有什么作用?繼承可以將另一個(gè)function上的prototype拷貝過來,當(dāng)想做多個(gè)屬性的面包的時(shí)候會(huì)...
    StarLikeRain閱讀 302評(píng)論 0 0
  • 1. apply、call 、bind有什么作用,什么區(qū)別? call ,apply的作用:調(diào)用一個(gè)函數(shù),傳入函數(shù)...
    Rising_suns閱讀 434評(píng)論 0 0
  • Chapter 6 面向?qū)ο蟮某绦蛟O(shè)計(jì) 理解對(duì)象 使用對(duì)象字面量語法創(chuàng)建對(duì)象var person = { n...
    云之外閱讀 675評(píng)論 0 1
  • 創(chuàng)建對(duì)象 工廠模式 這樣做就找不到原型關(guān)系了,沒有解決對(duì)象識(shí)別問題。 構(gòu)造函數(shù) 解決了對(duì)象識(shí)別問題,但多個(gè)對(duì)象實(shí)例...
    卿可津閱讀 309評(píng)論 0 0

友情鏈接更多精彩內(nèi)容