前端面試之js總結之創(chuàng)建對象

函數(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);

參考資料:

冴羽大神的博客
紅寶書(第三版)
繼承與原型鏈

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • ??面向對象(Object-Oriented,OO)的語言有一個標志,那就是它們都有類的概念,而通過類可以創(chuàng)建任意...
    霜天曉閱讀 2,265評論 0 6
  • 普通創(chuàng)建對象和字面量創(chuàng)建對象不足之處:雖然 Object 構造函數(shù)或對象字面量都可以用來創(chuàng)建單個對象,但這些方式有...
    believedream閱讀 2,593評論 2 18
  • 博客內容:什么是面向對象為什么要面向對象面向對象編程的特性和原則理解對象屬性創(chuàng)建對象繼承 什么是面向對象 面向對象...
    _Dot912閱讀 1,545評論 3 12
  • 面向對象的語言有一個標志,那就是它們都有類的概念,而通過類可以創(chuàng)建任意多個具有相同屬性和方法的對象。ECMAScr...
    DHFE閱讀 1,102評論 0 4
  • 在三亞的天涯海角,她坐在一塊大石頭上,眺望遠方的大海,若有所思。猛然,大雨傾盆,她不知道往哪躲,渾身淋得濕透。她在...
    小考拉俱樂部閱讀 276評論 2 3

友情鏈接更多精彩內容