1.function的静态变量

  <script type="text/javascript">
        function Universe() {
            this.name = "hongda";
            this.age = 28;
            if (Universe.instance) {
                return Universe.instance;
            }
            Universe.instance = this; 
        }
        var uni = new Universe();
        var uni2 = new Universe();
        console.log(uni === uni2);
    </script>

2.重写function的构造函数(其实就是把function覆盖)

 <script type="text/javascript">
        function Universe() {
            this.name = "hongda";
            this.age = 28;
            var instance = this;
            Universe = function () {
                return instance;
            }
        }
        var uni = new Universe();
        var uni2 = new Universe();
        console.log(uni === uni2);
    </script>

3.封装

        var Universe;
        (function () {
            var instance;
            Universe = function () {
                if (instance) {
                    return instance;
                }
                instance = this;
                this.name = "hongda";
                this.age = 28;
            };
        } ());
        var uni = new Universe();
        var uni2 = new Universe();
        console.log(uni === uni2);

 

http://www.cnblogs.com/TomXu/archive/2012/02/20/2352817.html

相关文章:

  • 2022-03-10
  • 2021-10-29
  • 2021-05-04
  • 2021-05-28
  • 2021-12-18
  • 2021-06-19
  • 2021-10-13
  • 2022-02-02
猜你喜欢
  • 2021-07-23
  • 2021-06-17
  • 2022-12-23
  • 2021-05-26
  • 2022-03-09
  • 2021-09-15
相关资源
相似解决方案