技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> JavaScript --> JavaScript Creating Objects Other Pattern

JavaScript Creating Objects Other Pattern

浏览:1560次  出处信息

    前面的日志中,抄了好多创建JavaScript对象的方法,包括工厂模式、原型模式、构造函数模式、混合模式、动态原型模式等。好了,大家快看烦了,日志也快结束了。

    书中还为我们介绍了另外两种创建JavaScript对象的方法:寄生构造器模式和持久构造器模式。大家再忍耐一下,让我抄完~~

Parasitic Constructor Pattern

    当其他创建对象的方法都不适合于你时,寄生构造器模式通常能满足你。该模式的主要意图是通过创建一个构造函数,在构造函数内部创建另一个对象并为其赋予方法,在最后将这个对象返回。

    没错,就是用构造函数的模式来写工厂方法,你真聪明。让我们来看一个小例子:

function Person(name, age, job){

       var o = new Object();

       o.name = name;

       o.age = age;

       o.job = job;

       o.sayName = function(){

           alert(this.name);

      };    

       return o;

    }

                      

    var person = new Person("Nicholas", 29, "Software Engineer");

    person.sayName();  //"Nicholas"

    在构造函数内部,我们声明一个新的对象,并为其赋予方法然后将之返回。除了使用new运算符让我们知道这是一个构造函数模式,否则一切看起来都是一个工厂,不是吗?

    When a constructor doesn’t return a value, it returns the

    new object instance by default. Adding a return statement at the end of a constructor allows you to

    override the value that is returned when the constructor is called.

    在返回的新对象和构造函数之间没有任何关系,这也就意味着你不能通过instanceof运算符来确定对象类型。而且其同工厂模式一样,对函数存在重复申明的问题,大大滴浪费了我们的内存空间。

    一般这种模式应用并不是很广泛,至少我很少看见这种创建对象的方式,不过并不代表其一无是处。

    书中给了一个例子,当你不想破坏数组的原型对象时,却想让其有一个适应你当前程序的通用方法(即,所有数组都可以拥有的方法),一般可以这样做

function SpecialArray(){      

                      

       //create the array

       var values = new Array();

      

       //add the values

       values.push.apply(values, arguments);

      

       //assign the method

       values.toPipedString = function(){

           return this.join("|");

       };

      

       //return it

       return values;        

    }

                      

    var colors = new SpecialArray("red", "blue", "green");

    alert(colors.toPipedString()); //"red|blue|green"

    此段代码不多解释。

Durable Constructor Pattern

    持久构造器模式和刚才介绍的寄生构造器模式看起来很像。只是在内部对象的方法里不使用this关键字,创建对象的实例时也不使用new运算符而已。先让我们看一下重写后的例子吧

function Person(name, age, job){

                      

       //create the object to return

       var o = new Object();

                      

       //optional: define private variables/functions here

                      

       //attach methods

       o.sayName = function(){

           alert(name);

       };    

                      

       //return the object

       return o;

    }

    var person = Person("Nicholas", 29, "Software Engineer");

    person.sayName();  //"Nicholas"

    该种创建对象的方法适用于对数据安全要求很高的情况下,除了公有方法,没有任何途径可以获取内部数据,通过闭包严格的保证了数据的安全性。缺点嘛同上一模式。呵呵。

建议继续学习:

  1. 软件架构模式的种类    (阅读:2794)
  2. 让生活变简单的简单网站    (阅读:2193)
  3. 状态模式和策略模式的比较    (阅读:1754)
  4. JavaScript Dynamic Prototype Pattern    (阅读:1702)
  5. 关于经营模式    (阅读:1579)
  6. 聊聊设计模式(4):装饰模式    (阅读:1241)
  7. Python创建单例模式的三种方式    (阅读:1128)
  8. [译]Go开发中一些有用的模式    (阅读:746)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1