zoukankan      html  css  js  c++  java
  • several way to create object in javascript

     //factory model:
        function createPerson(name, age, job) {
            var o = new Object();
            o.name = name;
            o.age = age;
            o.job = job;
            o.sayName = function () { alert(this.name); };
            return o;
        }
    
        //constructor model:
        function Person(name, age, job) {
            this.name = name;
            this.age = age;
            this.job = job;
            this.sayName = function () { alert(this.name); };
        }
    
        //prototype model
        function Person() { }
        Person.prototype.name = "roy";
        Person.prototype.age = 29;
        Person.prototype.job = "software engineer";
        Person.prototype.sayName = function () { alert(this.name); };
    
        //mix constructor model and prototype model
        function Person(name, age, job) {
            this.name = name;
            this.age = age;
            this.job = job;
            this.friends = ["roy","angel"];
        }
        Person.prototype = {
            constructor: Person,
            sayName: function () { alert(this.name); }
        }
    
        //dynamically prototype model
        function Person(name, age, job) {
            //properties
            this.name = name;
            this.age = age;
            this.job = job;
    
            //methods
            if (typeof this.sayName != "function") {
                Person.prototype.sayName = function () { alert(this.name); };
            }
        }
    
        //parasitic constructor model
        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;
        }
    
        //durable object
        function Person(name, age, job) {
            //create return object
            var o = new Object();
    
            //define private variable and method
            var xx;
            var yy;
    
            //add method
            o.sayName = function () { alert(name); };
    
            //return object
            return o;
        }
  • 相关阅读:
    Java 基础巩固:装箱拆箱 你真的熟悉吗
    Java数据类型的转换:隐式(自动)转换与强制转换
    oozie学习笔记
    flume学习笔记
    sqoop学习笔记
    Oracle故障排查之oracle解决锁表问题
    zookerper安装部署
    hadoop HA架构安装部署(QJM HA)
    hadoop第一部分-安装、测试
    hadoop完全分布式安装部署-笔记
  • 原文地址:https://www.cnblogs.com/ongoing/p/3078797.html
Copyright © 2011-2022 走看看