Hi,nice to see you again and today let us see some basic concepts in Javascript
First of all let me show you the source code to print "Hello World" in javascript.
Next let me explain you some facts about JSON.
JSON, which is so called Javascript object notation has become a very popular data interchange format.
First of all we will see how to create JSON objects and to add properties and function to an object.
- JSON is exactly look simmilar to the object notation in other programming languages but there are some standards to follow in javascript.
- JSON objects are surrounded by curly braces {}.
- JSON objects are written in key/value pairs.
- Keys must be strings, and values must be valid JSON data type (string,number,object,array,boolean or null)
- Keys and values seperated by a colon.
- Each key/value pair is seperated by a comma.
So the output of the above source code is given below,
Accessing object in javascript can also be done as given below.
So you can see both methods of accessing an object gives you the expected output in the console.
So next let us see how "this" keyword works in javascript,
I hope you can remember that "this" keyword in java acts as a reference variable that refers to the current object, but in javascript "this" keyword works somewhat different.
Now let us see how the the stuffs works,
This in a method
In an object method, this refers to the "owner" of the method.
This alone
When used alone, the owner is the Global object, so this refers to the global object [object window].
In the browser window the global, window object is displayed.
This in a function
In a javascript function, this refers to the global object [object window].Next let us have a look on Scopes in javascript.
Javascript language have two types of scopes,
- Global Scope
- Local scope
The variables defined outside of a function are in the global scope. Variables defined in the global scope can be accessed from any other scope.
Local Scope
Variables defined inside a function are in local scope.Each and every function when invoked creates a new scope.Therefore variables having the same name can be used in different functions.This is because those variables are bound to their own scope, and not accessible in other functions.
Block statements
Block statements such as if and switch conditions or for and while loops, donot create a new scope but will remain in the scope they were already in.
ECMAScript 6 has introduced the let and const keywords which can support local scope inside block statements.
Let keyword
- Redeclartion of let variable with var in the same scope or in the same block is not allowed.
- Redeclartion of let variable in the same scope or in the same lock is allowed.
Const keyword
- Const variable is same as the Let variable but only difference is Let variables cannot be reassigned. But keep in mind that it doesnot define a constant variable but a constant reference to a value.
So next let us go for a another new concept,
Object Prototypes
Object prototype is a concept on which the inheritance in javascript is implemented.
Every normally created object, array, and function has a prototype chain of
__proto__
properties ending with object.prototype at the top. This is why they’re all considered first-class objects in JavaScript.
Hope to see you with another blog post,
Till then,
Good bye
Till then,
Good bye
Comments
Post a Comment