• 创建对象的几种设计模式


    组合使用构造函数跟原型模式:构造函数模式用于定义实例化属性,原型函数模式用于定义方法和共享的属性,集两种之长,是目前使用最广泛,认同度最高的一种模式

        function myObject(name,message){
    		this.name=name.toString();
    		this.message=message.toString();
    	}
    	myObject.prototype={
    		getName:function(){
    			return this.name;
    		},
    		getMessage:function(){
    			return this.message;
    		}
    	};
    	myObject.prototype.getNAME=function(){
    		return this.name;
    	}
    	myObject.prototype.getMESSAGE=function(){
    		return this.message;
    	}
    	var myObject1=new myObject('wenwen','hrlpe');
        
        myObject1.name//wenwen

     构造函数模式:每一次实例化,该对象内部函数都会被创建一遍

           1.没有显式的创建对象

           2.直接将属性和方法赋予了this对象

           3.没有return语句,

    检测对象类型: p instanceof Object;

        function MyObject(name, message) {
    	  this.name = name.toString();
    	  this.message = message.toString();
    	  this.getName = function() {
    	    return this.name;
    	  };
    
    	  this.getMessage = function() {
    	    return this.message;
    	  };
    	}
    	var p=new MyObject('wewweb','help us');
    	var p1=new MyObject('wewweb','help us');
    
    
            p1.name
            "wewweb"
            p.name
            "wewweb"
        
            p.getMessage()
            "help us"
            p1.getMessage()
            "help us"        
    

    稳妥构造函数模式:没有公共属性,其方法也不引用this的对象,安全环境

        function MyObject(name, message) {
            var o=new Object();
            o.getName=function(){
                return name;
            }
            return o;
        }
        var p=new MyObject('wenwen','wenwe');
        p.getName();
    
    
            p.getName()
            "wenwen"
            p.name
            undefined
            p.message
            undefined        

    寄生构造函数模式:

        function MyObject(name, message) {
              var o=new Object();
              o.name=name;
              o.message=message;
              o.getName=function(){
                  return this.name;
              }
              return o;
            }
            var p=MyObject('wewweb','help us');
            console.log(p.getName()); 

    工厂模式:不能解决对象识别类型的问题,因为全部都是Object,随后出现了构造函数模式

    	function MyObject(name, message) {
    	  var o=new Object();
    	  o.name=name;
    	  o.message=message;
    	  o.getName=function(){
    	  	return this.name;
    	  }
    	  return o;
    	}
    	var p=MyObject('wewweb','help us');
    	var p1=MyObject('wewwevvb','help us');
    

     原型模式:使用原型对象的好处就是可以让所有的对象实例共享它所包含的属性和方法

    	function MyObject() {
    
    	}
    	MyObject.prototype.name='wenwe';
    	MyObject.prototype.age='23';
    	MyObject.prototype.getName=function(){
    		return this.name;
    	}
    	var p=new MyObject();
    	var p1=new MyObject();


        

          p.name
          "wenwe"

          p.getName()
          "wenwe"

     动态原型模式:

        function MyObject(name, message) {
             this.name = name.toString();
              this.message = message.toString();
              this.getName = function() {
                return this.name;
              };
    
              if(typeof this.getName !='function'){
                  MyObject.prototype.getName=function(){
                      return this.name;
                  }
              }
    
            }
        var p=new MyObject('wenwen','wenwe');
        p.getName();

      p.getName()
      "wenwen"
      p.name
      "wenwen

     
  • 相关阅读:
    奇虎董事长周鸿祎:谁说没钱不能创业
    分析.NET基础库源码,学习Stream类及相关类
    Why need two IF in singleton pattern in the multiple threads scenario
    It's bad design that leveraging singleton design pattern in db connection
    Asp.net mvc 3 JSONResult return array
    System.Web.HttpContext.Current vs. ControllerContext.HttpContext (almost the same)
    Nhibernate HQL example paging and avoid sql injection
    Asp.net mvc 3 JSON complext object mapping
    Nhibernate configuration in app.config with log4net enabled 0 of 4
    Setup and run a simple nhibernate example
  • 原文地址:https://www.cnblogs.com/lwwen/p/5795438.html
Copyright © 2020-2023  润新知