JavaScript ES6 Classes and Arrow Functions

I try to understand difference between function expression, shorthand method definition, arrow function and bind().

Notice that I defined sayGoodBye() function in constructor, it means that whenever i create a new instance from this Person class, sayGoodBye() function will be defined for every instance.

sayHello() function is defined as a class property. But, as you can see in the following screenshot, class properties are also defined in constructor so class properties are just a sugar syntax.

getFullName() function is defined as shorthand method definition. But, Babel didn't move function definition in class constructor. So, if we use shorthand method definition in class, it will be a prototype function .

It mean they are only defined once, instead of once per instance.

Notice that in below screenshot, getFullName() function is in prototype of Person class, but sayHello() and sayGoodBye() are not.

Continue reading

JavaScript Three Ways of Handling Asynchronous Operations

There is one more post about Promise and Async/Await usage in the following link.

There are 3 ways to handle asynchronous function calls.

Using Callbacks

Continue reading

Node.js Mocha and Chai

Mocha is a test framework to run tests.

https://mochajs.org/#assertions

Chai is an assertion library and you can find full documentation in the following link.

https://www.chaijs.com/api/bdd/

There are three type of assertion libraries in Chai.

  • Expect
  • Should
  • Assert

expect and should use the same chainable language to construct assertions.

Assume that expect library is used like below

expect(firstName).to.be.a('string');
expect(firstName).to.equal('kenan');
expect(firstName).to.have.lengthOf(5);
expect(beverages).to.have.property('tea').with.lengthOf(3);

Above code could be written with should library like below.

firstName.should.be.a('string');
firstName.should.equal('kenan');
firstName.should.have.lengthOf(5);
beverages.should.have.property('tea').with.lengthOf(3);

You can find more detailed usage with Express, Mocha and Chai in the following repository

https://github.com/nodejs-projects-kenanhancer/express-demo.git

Continue reading

Find Interaction Algorithm

FindIntersection(strArr) read the array of strings stored in strArrwhich will contain 2 elements: the first element will represent a list of comma-separated numbers sorted in ascending order, the second element will represent a second list of comma-separated numbers (also sorted). Your goal is to return a comma-separated string containing the numbers that occur in elements of strArr in sorted order. If there is no intersection, return the string false

Demo 1

Continue reading

TypeScript Syntax

Type Aliases, Union Types, Literal Types, Index Signature

You can find different usages of Type Aliases in the following demo code.

Type Aliases is defined with type word.

Union Types is defined with the | character to separate the different possible types.

Literal Types can be string, number or boolean

type SelectedEvent = "Click"; // string literal type

const buttonEvent: SelectedEvent = "Click";
type Color = "Red" | "Green" | "Blue"; // string literal type with union type

const buttonForeColor: Color = "Red";
type num = 1 | 3 | 5 | 7 | 9; // number literal type with union type
type bool = true | false; // boolean literal type with union type

type TRUE = true;

type FALSE = false;

Index Signature is defined as below

type Dictionary = {
    [index: string]: any;
};

const person: Dictionary = {
    "firstName": "Kenan",
    "lastName": "Hancer",
    "age": 36
};
interface StringArray {
    [index: number]: string;
}

const names: StringArray = ["Bob", "Fred"];

let name1: string = names[0];

Type Aliases can be used with Union Types and Literal Types as below

type obj = {success: true} | {success: false}; // object
type Result<T> = { success: true, value: T } | { success: false, error: string | number }; // object
type PersonCommonFields = { firstName: string, lastName: string };
type Person = PersonCommonFields & { isDeleted: true | false };
type Name = string; // simple type
type NameResolver = () => string; // function
type NameOrResolver = Name | NameResolver;

Demo1

Continue reading