js单列和继承的一些写法

------------------------
 <script type="text/javascript">
        /**
         * @descriptor 使用这样的继承方式是有问题的,books这种类型的变量是所有对象共享的啦,不会在自己的空间中创建book的空间
         * @param name
         * @param age
         * @constructor
         */
        var Person = function(name,age){
            this.name =name;
            this.age = age;
            this.books = [];
        }
        Person.prototype.say=function(){
            alert(this.name+","+this.age+this.books);
        }
       Person.prototype.getBooks=function(){
           return this.books;
       }
        var Student = function(){}
        Student.prototype = new Person();
        var s = new Student();
        s.getBooks().push("java");
        s.say();
        var s2 = new Student();
        s2.getBooks().push('javascript');
        s2.say();
    </script>

-----------------------解决办法--------------------------------
   <script type="text/javascript">
        /**
         * @descriptor 使用这样的继承方式是有问题的,books这种类型的变量是所有对象共享的啦,不会在自己的空间中创建book的空间
         * @param name
         * @param age
         * @constructor
         */
        var Person = function(name,age){
            this.name =name;
            this.age = age;
            this.books = [];
        }
        Person.prototype.say=function(){
            alert(this.name+","+this.age+this.books);
        }
       Person.prototype.getBooks=function(){
           return this.books;
       }
        var Student = function(name,age,no){
            Person.call(this,name,age);//这句话相当于
             /*this.name =name;
             this.age = age;
             this.books = [];*/
        }
        Student.prototype = new Person();
        var s = new Student();
        s.getBooks().push("java");
        s.say();
        var s2 = new Student();
        s2.getBooks().push('javascript');
        s2.say();
    </script>

----------------------------解决方案2------------------------------------
<script type="text/javascript">
        function ExtndsUtils(SubClass,SuperClass){
            var temp = function(){};
            temp.prototype = SuperClass.prototype;
            SubClass.prototype = new temp();
            SubClass.superClass = SuperClass.prototype;
        }
    </script>
    <script type="text/javascript">
        /**
         * @descriptor 使用这样的继承方式是有问题的,books这种类型的变量是所有对象共享的啦,不会在自己的空间中创建book的空间
         * @param name
         * @param age
         * @constructor
         */
        var Person = function(name,age){
            this.name =name;
            this.age = age;
            this.books = [];
        }
        Person.prototype.say=function(){
            alert(this.name+","+this.age+this.books);
        }
       Person.prototype.getBooks=function(){
           return this.books;
       }
        var Student = function(name,age,no){
            Student.superClass.constructor.call(this,name,age);//这句话相当于
             /*this.name =name;
             this.age = age;
             this.books = [];*/
        }
        ExtndsUtils(Student,Person);
        var s = new Student();
        s.getBooks().push("java");
        s.say();
        var s2 = new Student();
        s2.getBooks().push('javascript');
        s2.say();
    </script>

--------------------------单列的定义方式-----------------------------------------
/**
 * Created by canton_cowboy on 14-7-13.
 */
/**
 * 一般的单列定义方式
 */
var MyNameSpace={}
MyNameSpace.test1=(function(){
    /**
     * 私有的属性和方法一般都是以_开头
     */
    function _privF1(){
    }
    function _privF2(){}
    return {
        pubF:function(){
            //私有方法是外面无法访问的
            _privF1();
            _privF2();
        }
    }
})();

单例模式一般有以下几种写法: 1. 懒汉式单例模式:在第一次使用时才创建实例,线程不安全,可以使用synchronized关键字进行同步,但是会影响性能。 ```java public class Singleton { private static Singleton instance = null; private Singleton() {} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } ``` 2. 饿汉式单例模式:在类加载时就创建实例,线程安全,但是无法进行延迟加载。 ```java public class Singleton { private static Singleton instance = new Singleton(); private Singleton() {} public static Singleton getInstance() { return instance; } } ``` 3. 双重校验锁单例模式:使用双重校验锁机制,既能保证线程安全,又能够进行延迟加载。 ```java public class Singleton { private static volatile Singleton instance = null; private Singleton() {} public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } } ``` 4. 静态内部类单例模式:使用静态内部类实现单例模式,既能保证线程安全,又能够进行延迟加载。 ```java public class Singleton { private Singleton() {} private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE; } } ``` 以上是几种常见的单例模式写法。需要注意的是,单例模式在多线程环境下需要特别小心,需要保证线程安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值