js创建单例实例对象

先创建一个函数:



function Student(){
}
//添加静态方法
Student.getSingleStudent=function(){
 if(Student.prototype.lock==='undefinde'){
       Student.prototype.singInstance=new Student;
       Student.prototype.lock=true;
      
  }
  return Student.prototype.singInstance;
}


var  student1=Student.getSingleStudent();

var student2=Student.getSingleStudent();

alert(student1===student2)

弹出true

发布了28 篇原创文章 · 获赞 16 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/sunboylife/article/details/103795727
今日推荐