zoukankan      html  css  js  c++  java
  • js创建对象的几种方式

    一、工厂模式

    function createStudent(name,age){
                 var o=new Object();
                o.name=name;
                o.age=age;
                o.myName=function(){
                    alert(this.name);                    
                };
                return o;            
             }
             var student1 =createStudent('yjj',15);
             var student2 = createStudent('fff',18);
    //问题: 工厂模式没有解决对象识别的问题,不能确定一个对象的类型

    二、构造函数模式

      function Student(name,age){
                this.name=name;
                this.age=age;
                this.myName=function(){
                    alert(this.name);                    
                };    
             }
             var student1_ = new Student('yjj',15);
             var student2_ = new Student('fff',18);
              
            
             //new 关键字的作用
             //1.创建一个对象
             //2.将构造函数的作用域赋给新对象,this指向了新对象
             //3.执行构造函数中的代码,为新对象添加熟悉
             //4.返回新对象
             
             //问题: 每个方法都要在每个实例上重新创建一遍

    三、构造函数模式+原型模式

     function Student(name,age){
                 this.name=name;
                this.age=age;
                
             }
             Student.prototype.myName=function(){
                    alert(this.name);                    
             };    
             
             var student1__ = new Student('yjj',15);
             var student2__ = new Student('fff',18);
             student1__.myName();

    四、动态原型模式

    function Student(name,age){
                 this.name=name;
                this.age=age;
                if(typeof this.myName!="function"){//第一次进入
                     Student.prototype.myName=function(){
                        alert(this.name);                    
                     };                    
                }
                
             }
           var student1___ = new Student('yjj',15);
             student1___.myName();

    五、寄生构造函数模式

     function Student(name,age){
                var o = new Object();
                o.name=name;
                o.age=age;
                o.myName=function(){
                    alert(this.name);                    
                };                    
                return o; 
             }
             var student1____ = new Student('yjj',15);
             student1____.myName();
             //这种模式的基本思想是创建一个函数,该函数的作用仅仅是封装创建对象的代码,然后再返回新创建的对象。

    六、稳妥构造函数模式

     function Student(name,age){
                var o = new Object();
                var name=name;
                var age=age;
                o.myName=function(){
                    alert(name);                    
                };                    
                return o; 
             }
              var student1_____ = new Student('yjj',15);
             student1_____.myName();
             //没有公共属性,而且其他方法也不用引用this的对象
  • 相关阅读:
    前端了解即可:postman(接口测试)的使用
    ES6——TDZ(暂时性死区)
    Centos自动安装openssh及openssl脚本并隐藏版本号
    ELK学习链接
    Centos7 中使用搭建devpi并且使用Supervisor守护进程
    ansible系列
    iperf3网络测试工具
    Centos6.9下PXE安装centos 7
    CentOS 6.9下PXE+Kickstart无人值守安装操作系统
    django学习篇
  • 原文地址:https://www.cnblogs.com/yangjingqi/p/4337420.html
Copyright © 2011-2022 走看看