• javascript 创建对象的几种方式


    1、通过new Object创建

    创建自定义对象的最简单方式就是创建一个Object 的实例,然后再为它添加属性和方法,如下所示。

    var person = new Object();
    person.name = "Nicholas";
    person.age = 29;
    person.job = "Software Engineer";
    person.sayName = function(){
    alert(this.name);
    };
    

    2、通过对象字面量创建对象

    var person = {
      name: "Nicholas",
      age: 29,
      job: "Software Engineer",
      sayName: function(){
      alert(this.name);
     }
    };
    

    3、通过构造函数模式创建

        function Person(name, age, job){
            this.name = name;
            this.age = age;
            this.job = job;
            this.sayName = function(){
                alert(this.name);
            };
        }
        var person1 = new Person("Nicholas", 29, "Software Engineer");
        var person2 = new Person("Greg", 27, "Doctor");
    

    4、通过原型模式创建

        function Person(){
        }
        Person.prototype = {
            constructor: Person,
            name : "Nicholas",
            age : 29,
            job : "Software Engineer",
            friends : ["Shelby", "Court"],
            sayName : function () {
                alert(this.name);
            }
        };
        var person1 = new Person();
        var person2 = new Person();
        person1.friends.push("Van");
        alert(person1.friends); //"Shelby,Court,Van"
        alert(person2.friends); //"Shelby,Court,Van"
        alert(person1.friends === person2.friends); //true
    

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

      function Person(name, age, job){
            this.name = name;
            this.age = age;
            this.job = job;
            this.friends = ["Shelby", "Court"];
        }
        Person.prototype = {
            constructor : Person,
            sayName : function(){
                alert(this.name);
            }
        }
        var person1 = new Person("Nicholas", 29, "Software Engineer");
        var person2 = new Person("Greg", 27, "Doctor");
        person1.friends.push("Van");
        alert(person1.friends); //"Shelby,Count,Van"
        alert(person2.friends); //"Shelby,Count"
        alert(person1.friends === person2.friends); //false
        alert(person1.sayName === person2.sayName); //true
    

    6、动态原型模式

     function Person(name, age, job){
            //属性
            this.name = name;
            this.age = age;
            this.job = job;
            //方法
            if (typeof this.sayName != "function"){
                Person.prototype.sayName = function(){
                    alert(this.name);
                };
            }
        }
        var friend = new Person("Nicholas", 29, "Software Engineer");
        friend.sayName();
    

    7、寄生构造函数模式  

        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 friend = new Person("Nicholas", 29, "Software Engineer");
        friend.sayName(); //"Nicholas"
    

    8、稳妥构造函数模式

        function Person(name, age, job){
            //创建要返回的对象
            var o = new Object();
            //可以在这里定义私有变量和函数
            //添加方法
            o.sayName = function(){
                alert(name);
            };
            //返回对象
            return o;
        }
    

      

      

     

      

      

     

      

  • 相关阅读:
    Unity3d TweenPosition.Begin()的使用浅析
    Unity3D 代理的使用及获取两个碰撞器的碰撞点
    Unity3D 调用GPS位置服务实现代码
    在控制台中输入msqyl一直报ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost'错误
    eclipse 提示错误The method of type must override a superclass method 的解决办法
    Linux高级命令
    Centos7安装及配置
    Linux基本命令
    多线程
    java总结
  • 原文地址:https://www.cnblogs.com/liujufu/p/5372986.html
Copyright © 2020-2023  润新知