函數(shù)創(chuàng)建對(duì)象的方法
工廠模式
function createPerson(name) {
var o = new Object();
o.name = name;
o.getName = function () {
console.log(this.name);
};
return o;
}
var person1 = createPerson('kevin');
缺點(diǎn):解決不了對(duì)象識(shí)別問(wèn)題缰猴,所有實(shí)例指向Object绊诲。
構(gòu)造函數(shù)模式
function Person(name,age,job){
this.name=name;
this.age=age;
this.job=job;
this.sayName=()=>{
console.log(this.name);
}
}
var person1=new Person("cc",20,"student");
var person2=new Person("yaya",20,"student");
優(yōu)點(diǎn):
優(yōu)點(diǎn):實(shí)例可以識(shí)別為一個(gè)特定的類型
缺點(diǎn):每個(gè)方法都要在實(shí)例上重新創(chuàng)建
優(yōu)化構(gòu)造函數(shù)模式
function Person(name,age,job){
this.name=name;
this.age=age;
this.job=job;
this.sayName=sayName;
}
function sayName(){
console.log(this.name);
}
var person1=new Person("cc",20,"student");
var person2=new Person("yaya",20,"student");
優(yōu)點(diǎn):解決了重復(fù)創(chuàng)建的問(wèn)題纷捞。
缺點(diǎn):定義了多個(gè)全局變量痢缎,沒(méi)有封裝可言币呵。
原型模式
function Person(name,age,job){
}
Person.prototype.name="cc"
Person.prototype.sayName=()=>{
console.log(this.name)
}
var person1=new Person();
優(yōu)點(diǎn):方法不會(huì)重建
缺點(diǎn):所有屬性和方法都能共享悉尾,不能初始化參數(shù)
原型模式優(yōu)化
function Person(name,age,job){
}
Person.prototype={
name:"cc",
sayName:()=>{
console.log(this.name);
}
}
var person1=new Person();
優(yōu)點(diǎn):封裝性更好了點(diǎn)
缺點(diǎn):原型的缺點(diǎn)+constructor
原型模式優(yōu)化
function Person(name,age,job){
}
Person.prototype={
name:"cc",
sayName:()=>{
console.log(this.name);
}
}
Object.defineProperty(Person.prototype,"constructor",{
enumerable:false,
value:Person
})
優(yōu)點(diǎn):constructor有了
缺點(diǎn):原型的缺點(diǎn)
組合使用構(gòu)造函數(shù)模式和原型模式
function Person(name) {
this.name = name;
}
Person.prototype = {
constructor: Person,
getName: ()=> {
console.log(this.name);
}
};
var person1 = new Person();
優(yōu)點(diǎn):每一個(gè)實(shí)例都有自己的一份實(shí)例屬性的副本突那,又有對(duì)方法的引用。節(jié)省了內(nèi)存构眯。
缺點(diǎn):封裝性還可以做到更好愕难?(本人覺(jué)得這個(gè)挺ok的)
動(dòng)態(tài)原型模式
function Person(name) {
this.name = name;
if (typeof this.getName != "function") {
Person.prototype.getName = () =>{
console.log(this.name);
}
}
}
var person1 = new Person();
不能在使用動(dòng)態(tài)原型模式時(shí),使用對(duì)象字面量重寫(xiě)原型。
寄生構(gòu)造函數(shù)模式
function Person(name) {
var o = new Object();
o.name = name;
o.getName = function () {
console.log(this.name);
};
return o;
}
var person1 = new Person('kevin');
//例子:封裝別的具有額外方法的數(shù)組
function SpecialArray() {
var values = new Array();
values.push.apply(values,arguments[i]);
values.toPipedString = function () {
return this.join("|");
};
return values;
}
不能用instanceof操作符判斷
穩(wěn)妥構(gòu)造函數(shù)模式
穩(wěn)妥對(duì)象:指沒(méi)有公共屬性猫缭,而且其方法也不引用this屬性葱弟。
與寄生構(gòu)造模式的不同是:
1)新創(chuàng)建的對(duì)象不使用this
2)不使用new構(gòu)造符調(diào)用構(gòu)造函數(shù)
function person(name){
var o = new Object();
o.sayName = function(){
console.log(name);
};
return o;
}
var person1 = person('kevin');
person1.sayName(); // kevin
person1.name = "daisy";
person1.sayName(); // kevin
console.log(person1.name); // daisy
這種模式也不能使用instanceof。
補(bǔ)充
語(yǔ)法結(jié)構(gòu)和Object.create()創(chuàng)建
var a = {a: 1};
// a ---> Object.prototype ---> null
var b = Object.create(a);
// b ---> a ---> Object.prototype ---> null
console.log(b.a); // 1 (繼承而來(lái))
var c = Object.create(b);
// c ---> b ---> a ---> Object.prototype ---> null
var d = Object.create(null);
// d ---> null
console.log(d.hasOwnProperty); // undefined, 因?yàn)閐沒(méi)有繼承Object.prototype
class關(guān)鍵字創(chuàng)建
"use strict";
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
class Square extends Polygon {
constructor(sideLength) {
super(sideLength, sideLength);
}
get area() {
return this.height * this.width;
}
set sideLength(newLength) {
this.height = newLength;
this.width = newLength;
}
}
var square = new Square(2);