創(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)