Posts

ES5 and ES6

  Object in JS: The  object  is a complex data type that allows you to store collections of data as key-value pairs. Function in JS: A function is callable object that executes a block of code. Since functions are objects, so it is possible to assign them to variables var x = function() { } ·          There are no classes in JavaScript. Instead functions in JavaScript may be made to behave like constructors by preceding a function call with the new keyword (constructor pattern). ·          In JavaScript everything is an object except for the primitive data types (boolean, number and string), and undefined. On the other hand, null is actually an object reference even though you may at first believe otherwise. This is the reason typeof null returns "object". ·          The most important point however is that there are no classes in JavaScript b...

Angular Titbits: Concepts that make angular unique

Image
These are intermediate to advanced level concepts as a pre-understanding of angular is needed Real-life scenarios for using every concept mentioned below is included 1.        Custom Directive (Custom HTML elements!) Directives are angular syntax for HTML elements. 3 Types: Scenario for Attribute directive: To stop the user from copy pasting to an input field @Directive({ name: ‘[ preventCopyPaste ]’ }) @HostListener(‘paste’, [‘$event’]) blockPaste(event) { event.preventDefault() }   Use it in any input box: Confirm Email ID <input preventCopyPaste type=”text”> Note: Selector should be in square braces (selector: [preventCopyPaste] ). Use HostBinding and HostListener   2.        Custom Filter Note: for parameterized filter/Pipe, transform(texVal, paramVal), parameters start from 2nd argument ( {{title | prepend: 'PCBA') }} Pure Filter: Run the filter only for changes to its input val...

Jasmine/Karma- Writing Unit Test Cases for Angular

Image
What is Jasmine?   DOM-less simple JavaScript testing framework. Jasmine is a Behavior Driven Development testing framework for JavaScript. It does not rely on browsers, DOM, or any JavaScript framework. Thus, it's suited for websites, Node.js projects, or anywhere that JavaScript can run. What is Karma?   Spectacular Test Runner for JavaScript. Karma is not a testing framework, nor an assertion library. Karma just launches a HTTP server, and generates the test runner HTML file you probably already know from your favorite testing framework. So for testing purposes you can use pretty much anything you like In short, Jasmine: Javascript Testing Framework.   Can be used as TDD Karma: Browser testing. Used as a Rest Runner TESTING ANGULAR COMPONENTS Consider the below angular component: app.component.ts app.component.spec.ts TESTING SERVICES Angular services contain UI-independent reusable business logic of the application. myservice.service.ts The spec file of this...

Functional Programming in JavaScript

Image
There are different "paradigms" (just a fancy word which means "ways of programming"!) in JS programming like Object Oriented, Functional, Imperative etc Object Oriented involves all the OOPS concepts as we all know like Inheritance, Abstraction, Encapsulation, Polymorphism Imperative programming involves coding in an imperative way, l ike first do this, then do that etc Functional Programming Everything is expressed in the program in terms of “functions”. This function takes an input and returns the transformed data as output   Avoid side effects in functions, Use PURE functions: Any function that computes its output purely from the inputs it receives as arguments (should not modify other global data, change any other variable). Printing something to the console is also not “returning” an output. Basically, the function has to read the input, take that, and only that to compute an output. Functional programming means thinking of functions as purely as poss...