前端面试之js总结之创建对象

函数创建对象的方法

工厂模式

function createPerson(name) {
    var o = new Object();
    o.name = name;
    o.getName = function () {
        console.log(this.name);
    };

    return o;
}
var person1 = createPerson('kevin');

缺点:解决不了对象识别问题,所有实例指向Object。

构造函数模式

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");

优点:
优点:实例可以识别为一个特定的类型
缺点:每个方法都要在实例上重新创建

优化构造函数模式
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");

优点:解决了重复创建的问题。
缺点:定义了多个全局变量,没有封装可言。

原型模式

function Person(name,age,job){
}
Person.prototype.name="cc"
Person.prototype.sayName=()=>{
    console.log(this.name)
}
var person1=new Person();

优点:方法不会重建
缺点:所有属性和方法都能共享,不能初始化参数

原型模式优化
function Person(name,age,job){
}
Person.prototype={
    name:"cc",
    sayName:()=>{
        console.log(this.name);
    }
}
var person1=new Person();

优点:封装性更好了点
缺点:原型的缺点+constructor

原型模式优化
function Person(name,age,job){
}
Person.prototype={
    name:"cc",
    sayName:()=>{
        console.log(this.name);
    }
}
Object.defineProperty(Person.prototype,"constructor",{
    enumerable:false,
    value:Person
})

优点:constructor有了
缺点:原型的缺点

组合使用构造函数模式和原型模式

function Person(name) {
    this.name = name;
}

Person.prototype = {
    constructor: Person,
    getName:  ()=> {
        console.log(this.name);
    }
};
var person1 = new Person();

优点:每一个实例都有自己的一份实例属性的副本,又有对方法的引用。节省了内存。
缺点:封装性还可以做到更好?(本人觉得这个挺ok的)

动态原型模式

function Person(name) {
    this.name = name;
    if (typeof this.getName != "function") {
        Person.prototype.getName = () =>{
            console.log(this.name);
        }
    }
}

var person1 = new Person();

不能在使用动态原型模式时,使用对象字面量重写原型。

寄生构造函数模式

function Person(name) {
    var o = new Object();
    o.name = name;
    o.getName = function () {
        console.log(this.name);
    };
    return o;
}
var person1 = new Person('kevin');

//例子:封装别的具有额外方法的数组
function SpecialArray() {
    var values = new Array();
    values.push.apply(values,arguments[i]);
    values.toPipedString = function () {
        return this.join("|");
    };
    return values;
}

不能用instanceof操作符判断

稳妥构造函数模式

稳妥对象:指没有公共属性,而且其方法也不引用this属性。
与寄生构造模式的不同是:
1)新创建的对象不使用this
2)不使用new构造符调用构造函数

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()创建

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关键字创建

"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);

参考资料:

冴羽大神的博客
红宝书(第三版)
继承与原型链

你可能感兴趣的:(前端面试之js总结之创建对象)