This article is the first part of the tutorial on OOP in JavaScript and on creating a simpleJS component .
JavaScript is based on a simpleobject-oriented paradigm. An object is a collection of properties, and a property is an association between a name and a value. The value of a property can be a function, in which case the function will be called a method. In addition to the standard objects already defined in the browser, you can create your own objects.
All primitive types exceptnull
andundefined
treated as objects. They can be assignedsome properties, and they have all the characteristics of objects.
var simpleAlert = new Object(); // : , . var simpleAlert = {};
// var simpleAlert = new Object(); // simpleAlert.sa_default = "Hello World!"; simpleAlert.sa_error = "Error..."; simpleAlert.sa_success = "Success!"; // console.log(simpleAlert);
// alert(simpleAlert.sa_error);
... you can create objects using an object initializer. Using an initializer is sometimes called creating an object with literal notation . The object initializer name also corresponds to the terminology used inC ++ .
// var simpleAlert = { sa_default : "Hello World!", sa_error : "Error...", sa_success : "Success!" } // console.log(simpleAlert);
// var simpleAlert = { sa_default : "Hello World!", sa_error : "Error...", sa_success : "Success!", sa_fallback : function(){ console.log("Fallback"); } } // fallback'a simpleAlert.sa_fallback();
Define an object by writing a constructor. A good practice is to name a function with a capital letter. Create an object instance using the `new` keyword.
// function SimpleAlert( sa_default, sa_error, sa_success ) { this.sa_default = sa_default; this.sa_error = sa_error; this.sa_success = sa_success; } // var my_alert = new SimpleAlert( "Hello World!", "Error...", "Success!" );
console.log(my_alert); // console.log(my_alert.sa_error); // "Error..."
<div id="component"> <button id="default">Show Default Message</button> <button id="success">Show Success Message</button> <button id="error">Show Error Message</button> </div>
;(function( window ) { 'use strict'; })( window );
strict mode
. You can read about it here .window
to our anonymous function, so that we can later add SimpleAlert to the global scope.SimpleAlert
function. We also need it in the global scope. ;(function( window ) { 'use strict'; /** * SimpleAlert function */ function SimpleAlert( message ) { this.message = message; } // ... /** * SimpleAlert */ window.SimpleAlert = SimpleAlert; })( window );
(function() { /** * */ var default_btn = document.getElementById( "default" ); default_btn.addEventListener( "click", function() { var default_alert = new SimpleAlert("Hello World!"); } ); })();
Object.prototype
. MDN:All objects in JavaScript are descendants ofObject
; all objects inherit properties and methods fromObject.prototype
, although they can be overridden. The exception is an object with anull
prototype, for exampleObject.create(null)
).
init
function, and call it right after the object is created. This function will send a message to the console. ;(function( window ) { 'use strict'; /** * SimpleAlert function */ function SimpleAlert( message ) { this.message = message; this._init(); } /** * Initialise the message */ SimpleAlert.prototype._init = function() { console.log(this.message); } /** * Add SimpleAlert to global namespace */ window.SimpleAlert = SimpleAlert; })( window );
;(function( window ) { 'use strict'; /** * SimpleAlert function */ function SimpleAlert( message ) { this.message = message; this._init(); } /** * Initialise the message */ SimpleAlert.prototype._init = function() { this.component = document.getElementById("component"); this.box = document.createElement("div"); this.box.className = "simple-alert"; this.box.innerHTML = this.message; this.component.appendChild( this.box ); } /** * Add SimpleAlert to global namespace */ window.SimpleAlert = SimpleAlert; })( window );
message
variable inside the constructor and make it available inside the anonymous function using this.message = message
._init
function via this._init()
. Each time we create a new instance of an object, these two steps are automatically executed._init()
we declare the variables we need using the this
. Then we get access to the element #component
, and in it we place the div
containing the message. <div id="component"> <button id="default">Show Default Message</button> <button id="success">Show Success Message</button> <button id="error">Show Error Message</button> </div>
.simple-alert { padding: 20px; border: solid 1px #ebebeb; }
// COMPONENT // // The building blocks for our SimpleAlert component. //////////////////////////////////////////////////////////// ;(function( window ) { 'use strict'; /** * SimpleAlert function */ function SimpleAlert( message ) { this.message = message; this._init(); } /** * */ SimpleAlert.prototype._init = function() { this.component = document.getElementById("component"); this.box = document.createElement("div"); this.box.className = "simple-alert"; this.box.innerHTML = this.message; this.component.appendChild( this.box ); this._initUIActions; } SimpleAlert.prototype._initUIActions = function() { } /** * SimpleAlert */ window.SimpleAlert = SimpleAlert; })( window ); // EVENTS // // // . //////////////////////////////////////////////////////////// ;(function() { /** * Show default */ var default_btn = document.getElementById( "default" ); default_btn.addEventListener( "click", function() { var default_alert = new SimpleAlert("Hello World!"); } ); /** * Show success */ var default_btn = document.getElementById( "success" ); default_btn.addEventListener( "click", function() { var default_alert = new SimpleAlert("Success!!"); } ); /** * Show error */ var default_btn = document.getElementById( "error" ); default_btn.addEventListener( "click", function() { var default_alert = new SimpleAlert("Error..."); } ); })();
hide()
method).Source: https://habr.com/ru/post/240375/
All Articles