Posts

Showing posts from May, 2022

Dart enumerations,classes and objects

 Enumerations Named list of related items enum PersonProperties { firstName , lastName , age } void test () {   print ( PersonProperties . firstName ); }    Switch is the prefreed way of doing enumerations   Classes class Person {   void run () {     print ( 'Running' );   }   void breathe () {     print ( 'Brething' );   } } void test () {   //we have to first create instance of class to use it or we will get error   final person = Person ();   person . run (); }  Objects final foo = Person(); Constructor class Person {   final String name ;   Person ( this . name ); } void test () {   final foo = Person ( 'him' );   print ( foo . name ); } Methods class Person {   final String name ;   Person ( this . name );   void printName () {     print ( 'I will now priny name of person' );     print ( name );   } } void test () ...

Sound Null safety in Dart

 Make any type of data nullable String? name ="himanshu" name="null" //this is acceptable but if we didn't add the ? than it would be invalid List<String?>? names =['foo','bar',null]; //This is accepatble because of ? after g names = null; //this is acceptable bceasue of  ? after > Cherry picking non nullable values   const String? firstName = null; void test () {   String ? firstName = null ;   String ? middleName = 'kumar' ;   String ? lastName = 'gogoi' ;   firstName ?? middleName ?? lastName ; } Since lastName won't be exacuted as the middleName can't never be null so there will be a warning and to remove it we can passs the values in argaument so that it can't never no whether middleName will be null or not void test ( String ? firstName , String ? middleName , String ? lastName ) {   firstName ?? middleName ?? lastName ; }     Null aware assignment operator void test ( String ? firstName , Stri...

Javascript Class (ES6 syntax)

 They are the syntax sugar which are the way cleaner of explaning Prototype innheritance class Account { constructor(name,initialBalance){ this.name=name; this.balance = initialBalance; } deposit(amount){ this.balance += amount; console.log(`Hello ${this.name} your balanve ${this.balance}`) } } const john = new Account('john',0); console.log(john)

Javasrcipt Object Creation Pattern

 Factory Pattern var peopleFactory = function(name,age,state){ var temp ={} temp.age=age; temp.name=name; temp.state=state; temp.printPerson = function(){ console.log(this.name+","+this.age + ","+ this.state) } return temp; } var person1 = peopleFactory('john',23,'CA'); var person2 = peopleFactory('himanshu',23,'CA'); person1.printPerson(); person2.printPerson();  Cosntructor Pattern var peopleConstructor = function(name,age,state){ this.age=age; this.name=name; this.state=state; this.printPerson = function(){ console.log(this.name+","+this.age + ","+ this.state) } } var person1 = new peopleConstructor('john',23,'CA'); var person2 = new peopleConstructor('himanshu',23,'CA'); person1.printPerson(); person2.printPerson();   Everything is good with constructor patter the only problem is that if we create 1000 objects with it than I have to still write this.printPerson which is not good....

De-mystifying 'this' keyword of Javascript

 Hello today I will be talking about 'this' keyword of javascript.It is one of the most confusing term of javascript. Since it is quite a confusion thing I will be compiling stuffs based on the materials I have learnt from the internet so as to help myself in the future. 'this' keyword can be considered quite the literal this word is English alphabet atleast for the initial understanding. Let's consider a main table of a house. It will be termed in js langauge as this.table = "main table" If we console log window.table or this.table than we will be considering the main house's table console.log(window.table) //main table Now let consider the dinning table of the house. It will be  this.dinning={ table:"dinning table"} If we want to call it will be  this.dinning.table // dinning table   Now let consider a private room and in this case it will be a function and if we want to use there we can use as mentioned below let johnsroom = { table:'j...

Middleware in Node JS

 Middleware is the heart and soul of Node Js. It will be seen everywhere const express = require ( 'express' ); const app = express (); const logger = ( req , res , next ) => {     const method = req . method     const url = req . url     const time = new Date (). getFullYear ()     console . log ( method , url , time );     next () } app . get ( '/' , logger ,( req , res ) => {     res . send ( 'Home' )     }) app . listen ( 5000 ,() => {     console . log ( "server running on port 5000" ); }) You can see in the above example that Logger is our middleware in the simple way. However, we can use App.use() and if we use it the middleware automatically gets invoked in all the routes below it.  If we want to use multiple middleware functions than we can use it in array app . use ([ logger , authorize ]) Middelware with query string and conditional statement const authorize = ( r...