函數(shù)創(chuàng)建對象的方法
工廠模式
function createPerson(name) {
var o = new Object();
o.name = name;
o.getName = function () {
console.log(this.name);
};
return o;
}
var person1 = createPerson('kevin');
缺點:解決不了對象識別問題,所有實例指向Object。
構造函數(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)點:
優(yōu)點:實例可以識別為一個特定的類型
缺點:每個方法都要在實例上重新創(chuàng)建
優(yō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)點:解決了重復創(chuàng)建的問題。
缺點:定義了多個全局變量,沒有封裝可言。
原型模式
function Person(name,age,job){
}
Person.prototype.name="cc"
Person.prototype.sayName=()=>{
console.log(this.name)
}
var person1=new Person();
優(yōu)點:方法不會重建
缺點:所有屬性和方法都能共享,不能初始化參數(shù)
原型模式優(yōu)化
function Person(name,age,job){
}
Person.prototype={
name:"cc",
sayName:()=>{
console.log(this.name);
}
}
var person1=new Person();
優(yōu)點:封裝性更好了點
缺點:原型的缺點+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)點:constructor有了
缺點:原型的缺點
組合使用構造函數(shù)模式和原型模式
function Person(name) {
this.name = name;
}
Person.prototype = {
constructor: Person,
getName: ()=> {
console.log(this.name);
}
};
var person1 = new Person();
優(yōu)點:每一個實例都有自己的一份實例屬性的副本,又有對方法的引用。節(jié)省了內存。
缺點:封裝性還可以做到更好?(本人覺得這個挺ok的)
動態(tài)原型模式
function Person(name) {
this.name = name;
if (typeof this.getName != "function") {
Person.prototype.getName = () =>{
console.log(this.name);
}
}
}
var person1 = new Person();
不能在使用動態(tài)原型模式時,使用對象字面量重寫原型。
寄生構造函數(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)妥構造函數(shù)模式
穩(wěn)妥對象:指沒有公共屬性,而且其方法也不引用this屬性。
與寄生構造模式的不同是:
1)新創(chuàng)建的對象不使用this
2)不使用new構造符調用構造函數(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。
補充
語法結構和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 (繼承而來)
var c = Object.create(b);
// c ---> b ---> a ---> Object.prototype ---> null
var d = Object.create(null);
// d ---> null
console.log(d.hasOwnProperty); // undefined, 因為d沒有繼承Object.prototype
class關鍵字創(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);