📜 ⬆️ ⬇️

Trying a pen or thinking out loud

JavaScript Guaranteed Singleton


I remembered the pattern Singleton, and I thought - "Why not do it?".


  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

The object is with all its goodies like hiding the implementation, but there is no constructor.

PS Checked in FF (3.6, 3.5, 3.0, 2.0), Chrome 4, Safari 4, Opera (10.10, 9.64, 9.0), and IE (8, 7, 6).
')

Source: https://habr.com/ru/post/86039/


All Articles