Introduction to JavaScript
JavaScript
JavaScript(JS) is an interrupted, lightweight, or just-in-time programming language with a first-class function. JS is well known for a scripting language in web pages, and also it may use in many non-browser environments such as Node.js, Apache CouchDB, and Adobe Acrobat. It's a program that runs a single thread by default and prototype-based, multi-paradigm, supporting object-oriented, imperative, declarative styles, and also it is a dynamic language.
ECMAScript is the standard for javascript.
- As I said earlier javaScript programs run using a single thread and in javaScript, there are ways to create new threads, and even though it's called a single thread language.
- JavaScript is a non-blocking I/O because it does not wait to get completed I/O operation and instead it continues to execute the program.
- JavaScript is asynchronous.
- The eventing system in javascript manages its asynchronous operations.
Objects in JavaScript
In JavaScript, all the objects are instances of Object and a typical object inherits properties from Object. prototype. Just as many other programming languages object in javascript can be compared to objects in real life.
JavaScript objects are standalone entities with properties and types. For example, let's look at the car object. It's has a color, design, materials it's made of, weight and etc. In the same way, javascript object properties define their characteristics.
When creating a JavaScript object, the constructor function is used the 'new' keyword.
As for example look the below code segment,
const myCar = new Car();
Another way of object creation is literal ('{}') and these objects are considered as a singleton.
When an object initializer, comma-delimited list of zero or it will pairs more property names and associated values of an object enclosed in curly braces ('{}').
Example :
var myCar = {
make : 'Toyota',
model : 'Hilux'
year : 2020,
};
JavaScript support all the static variables and methods.
Prototypes in JavaScript
- The prototype is another object that refers to the JavaScript functions. It has a similarity between the class definition in the other languages.
- This prototype is another way of an object instance. This prototype object is used for creating objects, inheritance, and adding methods to a class.
- The prototype is the recommended way to create a class and as well as extend class because of the flexibility.
- A constructor function is used to create objects and it is also a function. Object instance also has a prototype and it may be the object instance, from the object is created.
- Object '_proto_' is where the object gets its properties from inherited.
- To object instance function prototype is used to inherit properties.
'this' keyword in JavaScript
'this' keyword in JavaScript is completely different from the other programming languages.
Inside the object, this keyword refers to the object itself.
For example :
const test = {
name: 'John',
getName: function() {
return this.name;
},
};
console.log(test.getName());
output: John
In the global context, 'this' refers to a global object and this behavior will change on strict mode.
When a function is passing 'this' keyword is being passed to another object then that object is will be referred to as the 'this' keyword. Most importantly it will not be the original object where the function was initially declared.
In callback and closures, this behavior is very noticeable.
References: Mozilla Developer Network-[MDN Web Docs]
Comments
Post a Comment