IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. It is a design pattern which is also known as Self-Executing Anonymous Function and contains two major parts. The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope. The second part is creating the immediately executing function expression (), through which the JavaScript engine will directly interpret the function.
Immediately Invoked Function Expression (IIFE)
Avoid Creating Global variable and Functions
As it doesn‟t define variable and function globally so there will be no name conflicts
Scope is limited to that particular function
Pass by Value
JavaScript arguments are passed by value: The function only gets to know the values, not the argument’s locations.
If a function changes an argument’s value, it does not change the parameter’s original value.
Changes to arguments are not visible (reflected) outside the function.
Pass by reference
In JavaScript, object references are values.
Because of this, objects will behave like they are passed by reference:
If a function changes an object property, it changes the original value.
Changes to object properties are visible (reflected) outside the function.
A variable that is declared outside a function definition is a global variable, and its value is accessible and modifiable throughout your program. In a web browser, global variables are deleted when you close the browser window (or tab) but remain available to new pages loaded into the same window.
Local Scope
A variable that is declared inside a function definition is local. It is created and destroyed every time the function is executed, and it cannot be accessed by any code outside the function. Inside a function, if a variable has not been declared with var it is created as a global variable.
Local Scope
If there is a function inside a function the inner function can access outer function‟s variables but the outer function can not access inner function‟s variables. Function arguments (parameters) work as local variables inside functions.
Block Scope
Variables declared with var do not have block scope.
Block Scope
Identifiers declared with let and const do have block scope.
Variable Hoisting
Hoisting is JavaScript’s default behavior of moving declaration to the top of the function, if defined in a function, or the top of the global context, if outside a function.
Variable Hoisting
A variable can be used before it has been declared. Only variable declarations are hoisted to the top, not variable initialization.
Closure
A closure is a function having access to the parent scope. It preserves the data from the outside.
A closure is an inner function that has access to the outer (enclosing) function‟s variables.
In JavaScript, we do not need to specify the type of the variable because it is dynamically used by the JavaScript engine. We can use var data type. It can hold any type of data like String, Number, Boolean, etc.
Primitive Data Type Non-Primitive Data type
String Object Number Array Boolean RegExp Undefine
A variable is an identifier or a name which is used to refer a value. A variable is written with a combination of letters, numbers and special characters _ (underscore) and $ (dollar) with the first letter being an alphabet.
Ex-
c, fact, b33, total_amount etc.
Rules
• Variable can contain combination of letters, digits, underscores ( _ ), and dollar signs ( $ ). • Must begin with a letter A-Z or a-z or underscore or dollar sign • A variable name cannot start with a number • Must not contain any space characters • JavaScript is case-sensitive • Can‟t use reserved keywords
name.js – This is our script file, which we created. The code of javascript is written inside devops.js. In which DevOps is the file name, .js is the extension. Javascript has an extension of .js
Type – This is also an attribute of the script tag, it tells the browser that it is javascript. Now it is not necessary to write it, you can leave it if you want.
Text/javascript – It defines what type these documents are.
document.write(“Hello World”); – This is a function which is displaying the data.
</script> – Closing Script tag.
How to link more than one External?
You can link more than one JavaScript file to a single document. Just need to write different script tags for that.
This function is used to write arbitrary HTML and content into the page. If we use this function after an HTML document is fully loaded, will delete all existing HTML. It is used only for testing purposes.
Ex:- document.write("Hello world");
document.write(variable);
document.write(4+2);
document.write("hello world . <br>");
document.write("hello world . <br>" + variable +"<br>");
Alert function in JavaScript
The data that will be displayed in it will be in the dialog box. You forcefully want to see this thing, then only this work will be done. In that case, you use a window. alert
An identifier is a name having a few letters, numbers, and special characters _(underscore). It is used to identify a variable, function, symbolic constants, and so on.
Ex:- X2 PI Sigma Matadd
Veriable in JavaScript
Variable can be any name, in which a combination of letters, numbers, and special characters can be written _()(underscore) and $(dollar). It is not necessary that there should be only a combination, it can only be a letter.
Ex:- c, fact, b33, total_amount ect.
Rules of Veriable in JavaScript
Variable can contain a combination of letters, digits, _()(underscore), and dollar signs($).
Must begin with a letter A-Z or a-z or underscore or dollar sign.
What was easy in any language was made core and what became difficult was made an advance. What happens in Core is of basic level and what happens in Advance is of upper-level.
Object Oriented Progrmming
Object-Oriented Programming is a Technic that solves the problem. In this, the code breaks the object. And you see your problem on the side of the object. In which you find your problem similar to the real-world problem and it is easy to maintain, and it has to be solved.
Class
The PHP class has a group of values. To manipulate whatever value you write inside the class, you also write operations in it. The class gives you this facility, that if the user does not need to know the information. So you can also hide the information. To use the application, you can keep it hidden for security purposes. Or if it will be accessed from the outside users or from an outside viewer then your application may be damaged. That’s why you can keep your application hidden, it gives class facility. You can define type using class.
Rule
The class name can be any valid label.
It can’t be PHP reserved world.
The class name must start with a letter or an underscore. Following by any number of letters, numbers, or underscore all this will work. Lwkin should not start with number.
class Classname
{
var $variable_name; (It is also called Data Member/Properties or Attribute.)
var $variable_name;
function Method_name()
{
Body of method;
}
function Method_name(parameter_list) (And these methods are called methods or member functions.)
{
Body of Method;
}
}
See your problem in the real world.
class Mobile
{
public $model;
function show Model (Sumber)
{
$this->model = $number;
echo "Model Number: $this->model";
}
}
Object
The object is a variable of class data type. Whenever you create an object, for that object, whatever is proper inside the class, a copy of it is prepared and it is prepared for that object. If there are 2 objects then 2 copies of that variable or that property will be created and one copy for the first object and a second copy for the second object. Whatever object you make, its copy will be ready and one copy will be distributed. But this is not the case with the member function. The member function is the same, all the objects share with it.
The new Operator is used to create an object.
Syntax:
class Mobile
{
public $model;
function show Model (Sumber)
{
$this->model = $number;
echo "Model Number: $this->model";
}
}
$samsung = new Mobile;
$samsung->showModel('A5');
$lg = new Mobile;
$lg->showModel('M30');
You will not need to write the class, again and again, you just have to create an object of it. And call this method with the help of that object. It will show its own model number.
Important thing
A class is of no importance unless it has an object.
Accessing class member using object
How would you make accessing a member of a class into a user object?
-> This operator is used to access the member of the class.
object_name->variable_name; $samsung->model;
access to method Object_name->method_name(); $samsung->showModel();
in parameter Object_name->method_name(parameter_list) $samsung->showModel(‘A8’);
$_GET and $_POST is generally used in the forms to transfer data from one file to another file. Both the variables are kind of same except when we need to keep our data secure we use $_POST and generally in the form we use $_POST.
We will see the explanation by a example:
Form.html
Testform.php
In this form we have used “get” method and we will see the output below:
As we can see we have used the method “get” in the form and in the output page of testform.php we can see the input given, also with that in the URL box we can see the details which we have provided in the form.html.
If we use “post” method in the form.html we will see the difference below:
As we can see in the URL box there is no such details provided which we can see while using “post” method.
In this tutorial we will learn about declaring global and local variable in PHP.
Initially we will see what is local variable and global variable with examples.
Here if we see the variable is declared inside the function “local” so it will work inside the local function only until we return the value inside the function.
In this we can see that the variable “a” is declared outside the function local, so it won’t work inside the function until we call it.
If we see the output for this we will see an error since “a” is a local variable and it will not work beyond the function “test”.
Now in this code if we see, variable “a” is declared globally and we will get an error if we call it inside any function.
To execute the global variable in the function we need to use predefined variable “global” inside the function.
We will see now that it will return value inside the function as well as outside the function.