美文网首页
构造器模式

构造器模式

作者: pws019 | 来源:发表于2017-04-30 13:02 被阅读24次

    constructor pattern


    在传统的面向对象编程语言中,构造函数经常用来初始化已经被分配内存的新的对象。在js中,一切皆对象,我们通常对对象的构造函数比较感兴趣。

    对象的构造函数常常用来创建指定类型的对象————当对象第一次被创建的时候,这个对象通过构造函数,接受用户参数,设置其内部的属性和方法。

    对象的创建通常有三种方法

    //等于 new Object();
    var newObject = {};
    
    //ES5的新语法,create接受两个参数,第一个问要继承的对象,没有传null,第二个为对象的属性描述符,例如读写权限等.
    var newObject = Object.create(Object.prototype);
    
    //根据传入的参数得到指定的对象,如果没传则得到一个单纯的对象。
    var newObject = new Object();
    // new Object({x:1,y:2}) => {x:1,y:2}
    // new Object(function(a){this.x = a}) => function(a){this.x=a}
    // new Object(1) =>new Number(1)
    

    //这里有四种方式给对象赋值

    //ES3
    //1. “.”语法
    newObject.someKey = "hello world";
    var value = newObject.someKey;
    
    //2.方括号语法[];
    newObject['someKey'] = 'Hello world';
    var value = newObject["someKey"];
    
    //3.ES5写法
    Object.defineProperty(newObject,"someKey",{
        value:"form more control of the proerty's behavior",
        writable:true,
        enumerable:true,
        configurable:true
    })
    
    //如果上面这种写法比较麻烦的话,可以定义一个函数
    var defineProp = function(obj,key,value){
        var config = {
            value:value,
            writable:true,
            enumerable:true,
            configurable:true
        };
        Object.defineProperty(obj,key,config);
    }
    //如果我们想创建一个“人”的对象
    var person = Object.create(Object.prototype);
    defineProp(person,"car","Delorean");
    defineProp(person,"dateOfBirth","1981");
    console.log(person);
    //=> Object {car: "Delorean", dateOfBirth: "1981", hasBeard: false}
    
    // 4. Object.defineProperties
    // 一次设置多个属性。
    Object.defineProperties( newObject, {
        "someKey": {
            value: "Hello World",
            writable: true
        },
        "anotherKey": {
            value: "Foo bar",
            writable: false
        }
    });
    

    基本的构造函数

    在早期js不支持类的概念,但是支持为对象指定构造函数
    通过简单的在构造函数调用前加上一个new的关键词作为前缀。
    可以告诉js,通过该函数实创建并例化一个对象。

    在这个构造函数中,this表示当前正要实例化的对象。
    再次来看一个对象的创建,一个基本的构造函数如下:

    function Car( model, year, miles ) {
    
        this.model = model;
        this.year = year;
        this.miles = miles;
    
        this.toString = function () {
            return this.model + " has done " + this.miles + " miles";
        };
    }
    
    
    //用法:
    
    // 我们可以创建并实例化一个car
    var civic = new Car( "Honda Civic", 2009, 20000 );
    var mondeo = new Car( "Ford Mondeo", 2010, 5000 );
    
    // 并且调用该实例化对象的方法
    console.log( civic.toString() );
    console.log( mondeo.toString() );
    

    以上是简单的构造函数模式,但是它仍然面临很多问题,
    一是不利于继承
    再一个便是每个实例都拥保留着一个同样的方法toString(),
    这显然不是我们想要的,我们希望的是。更理想的做法是将方法共享到CarType实例之间

    Constructors With Prototypes


    Functions(函数),像js中所有对象一样,都有一个prototype(对象)属性,当我们调用构造函数
    去创建一个新的对象时候,所有构造函数prototype对象上的属性,新对象都可以使用。
    通过这种方式,所有的Car对象都可以通过连接相同的原型对象,被创建,上面的例子被修改后如下:

    function Car( model, year, miles ) {
    
        this.model = model;
        this.year = year;
        this.miles = miles;
    
    }
    
    //在这里通过定义构造函数中的prototype属性,避免每个实例都拥重新定义这个属性。
    Car.prototype.toString = function () {
        return this.model + " has done " + this.miles + " miles";
    };
    
    // 用法:
    var civic = new Car( "Honda Civic", 2009, 20000 );
    var mondeo = new Car( "Ford Mondeo", 2010, 5000 );
    
    console.log( civic.toString() );
    console.log( mondeo.toString() );
    

    相关文章

      网友评论

          本文标题:构造器模式

          本文链接:https://www.haomeiwen.com/subject/lbqxtxtx.html