サンプルペンまたは大声で考えた

JavaScriptでシングルトンを保証



私はシングルトンのパターンを思い出し、「どうしてそんなことしないの?」と思いました。





  1. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor



  2. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor



  3. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor



  4. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor



  5. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor



  6. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor



  7. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor



  8. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor



  9. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor



  10. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor



  11. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor



  12. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor



  13. function a ( ) { // a.constructor = Function() var property ; this . property = function ( value ) { if ( value ) { property = value ; return property ; } else { return property ; } } } a = new a ( ) ; // a.constructor = a() var b = new a ( ) ; // Error: a is not a constructor





オブジェクトには、実装を隠すなど、すべての利点がありますが、コンストラクターはありません。



PSチェックインFF(3.6、3.5、3.0、2.0)、Chrome 4、Safari 4、Opera(10.10、9.64、9.0)、およびIE(8、7、6)。






All Articles