zoukankan      html  css  js  c++  java
  • 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;
        }
    

      

      

     

      

      

     

      

  • 相关阅读:
    图的概述
    "《算法导论》之‘排序’":线性时间排序
    “《算法导论》之‘查找’”:散列表
    如何使用VS2013本地C++单元测试框架
    “《算法导论》之‘查找’”:顺序查找和二分查找
    查找算法概述
    第二部分 位运算符、赋值运算符、三元及一元运算符和语句分类
    LINQ 的查询_联表、分组、排序
    第二部分 关系与比较运算符 、 自增与自减运算符、条件逻辑运算符
    LINQ to Sql系列一 增,删,改
  • 原文地址:https://www.cnblogs.com/liujufu/p/5372986.html
Copyright © 2011-2022 走看看