Learning Java Script Essentials
LEARNING JAVA SCRIPT ESSENTIALS
What is Java Script?
Java Script is a scripting language that enables you to create dynamically updating content, control multimedia, animate images etc.
JavaScript is a lightweight interpreted programming language.
What does Java Script do?
When we load a web page in browser, we are running your code (the HTML, CSS, and JavaScript) inside an execution environment (the browser tab). This is like a factory that takes in raw materials (the code) and outputs a product (the web page).The JavaScript is executed by the browser's JavaScript engine, after the HTML and CSS have been assembled and put together into a web page. This ensures that the structure and style of the page are already in place by the time the JavaScript starts to run.
This most common use of JavaScript is to dynamically modify HTML and CSS to update a user interface, via the Document Object Model API (DOM). If the JavaScript loaded and tried to run before the HTML and CSS were there to affect, then errors would occur.
How to Add Java Script to a page?
JavaScript is applied to your HTML page in a similar manner to CSS. Whereas CSS uses
<link>
elements to apply external stylesheets and <style>
elements to apply internal stylesheets to HTML, JavaScript only needs the <script>
element in HTML.- Internal JS : placed in between the
<head>
</head>. - External JS : keeping all the JS coding included in a seperate external file.
- Inline JS handlers : JS residing in a HTML tag under the script attribute.
Creating a JS variable
There are no variable types like int,
String in JavaScript(type-less). The way that creating JavaScript variables is given
below.
Here
the sample code display how to create a simple variable and way that values
assigning.
There is no data type
like Int , String in JS (JavaScript). So we can assign any numerical value or any
character type value to declared variable.
Creating a JS function
Creating a JSON object
JSON stands for Java Script Object Notation.
Here I create a JSON object call vehicle. It has 3 attributes. Two of them are initialize with numerical value. One attribute assign with function value.
Creating Java Script Classes
Creating a JS class is not like in Java. Using function and assigning properties by this key word we can create a class.
Prototypes
Prototype is actually a property in JS object. Using this property, we can add new properties to object constructor method. After we create our JS class we can add new properties (either attributes or methods/functions) using prototype property.
Closures
Closure is an inner function that has access to outer function variables. Closures are support for scope chaining.
Closure can access variables inside his
scope, then his outer function scope variables and global scope variables. At
line 19 we execute the function taxCalculator by passing 50 as parameter. At
line 8 the value I passed will assign to the declared variable called
taxPrecentage. Then I declare another variable called taxFunc and it contain
some function value. At line 16 taxFunc variable return to the consumer. It
means after execution of line 19 taxMeter assign with function value. That
means we can execute that function. You can see at line 20 I execute that
function value by passing 5000 as parameter. Value of the taxMeter is this,
function(amount)
{
return amount + amount *
taxPrecentage / 100;
}
So
after I pass 5000, the value of the amount variable is 5000. If we look at the
body of this function it expecting taxPrecentage value.
But
in this function scope it don’t have any variable like taxPrecentage. So that
it check his outer function scope.
Outer scope has declared variable
taxPrecentage. So it will access that value. I passed 50 to amount at the line
90. Therefore due to behaviour of closure taxPrecentage will get the value as 50.
Comments
Post a Comment