43

Once

Objective
  • Once

A Once class creates only one instance.

  1. function Once() {
  2.     return this.constructor._instance || (this.constructor._instance = this);
  3. }

The function Once creates an object whose constructor has a _instance property. If _instance is undefined, Once keeps the object just created this and returns it. Otherwise, Once returns the instance created previously.

  1. Once.prototype = Object.create(Objective.prototype);

Initializes the prototype of the function Once with a copy of the prototype of the function Objective (prototypal inheritance). The class Once inherits from the class Objective.

  1. Object.defineProperty(Once.prototype, 'constructor', { value: Once, enumerable: false, writable: true });

Renames the constructor.

See the article Inheritance in JavaScript on the website MDN Web Docs.

  1. Once.prototype.clone = function() {
  2.     return this;
  3. };

Redefines the clone method inherited from Objective class.

Test
  1. <?php head('javascript', '/objectivejs/Objective.js'); ?>
  2. <?php head('javascript', '/objectivejs/Once.js'); ?>

Adds the tags <script src="/objectivejs/Objective.js"></script> and <script src="/objectivejs/Once.js"></script> to the <head> section of the HTML document. head is a function of iZend. Adapt the code to your development environment.

  1. console.log(new Once() === new Once()); // true

Checks if the creations of 2 instances of Once return the same object.

  1. function Application(name) {
  2.     let app = Once.call(this);
  3.  
  4.     if (app === this)
  5.         this._name = name;
  6.  
  7.     return app;
  8. }
  9.  
  10. Application.prototype = Object.create(Once.prototype);
  11.  
  12. Object.defineProperty(Application.prototype, 'constructor', { value: Application, enumerable: false, writable: true });

Defines a class Application which inherits from the class Once. An instance of Application has a name.

IMPORTANT: The last instruction return app; specifically returns the unique instance returned by the constructor of the class Once.

  1. let app1 = new Application('foobar');
  2. let app2 = new Application('barfoo');

Creates 2 instances of Application with different names.

  1. console.log(app1.constructor.name); // Application
  2. console.log(app1 === app2);         // true
  3. console.log(app1._name);            // foobar

Displays the name of the constructor of an instance of Application. Checks that only one instance of Application has been created and that its name is the one passed as a parameter to the first instantiation.

  1. let app3 = app1.clone();
  2. console.log(app1 === app3);         // true

Checks that the clone of an instance of Application is the original instance.

Display the page generated by the file testOnce.phtml and check the trace in the console of the browser:

true
Application
true
foobar
true
SEE ALSO

Objective, Responder

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].