Skip to content

Instantly share code, notes, and snippets.

View jagmeet-chaudhary's full-sized avatar

Jagmeet Singh jagmeet-chaudhary

View GitHub Profile
@jagmeet-chaudhary
jagmeet-chaudhary / arrow-functions-this-behaviour.js
Created February 2, 2022 11:58
Difference between normal functions and arrow functions in terms of 'this'
var bob1 = {
name : "Jack",
friends : [],
getFriends : function() {
console.log(this.name);
this.friends.forEach(function(item){
console.log(this.name + " is friends with " + item); //this is undefined here
})
}
}
@jagmeet-chaudhary
jagmeet-chaudhary / classes.js
Created January 29, 2022 11:28
Classes and prototypal inheritance
class Employee {
constructor(empNo,name,department){
this.employeeNumber = empNo;
this.name = name;
this.department = department;
}
getEmployeeProfile(){
return this.name + "|" + this.department + "|" + this.employeeNumber;
}
@jagmeet-chaudhary
jagmeet-chaudhary / object-create.js
Created January 29, 2022 11:10
Prototypal inheritance and object create
var employee = {
name : '',
department : '',
employeeNumber : '',
getEmployeeProfile : function() {
return this.name + "|" + this.department + "|" + this.employeeNumber;
}
}
var jack = Object.create(employee);
@jagmeet-chaudhary
jagmeet-chaudhary / function-constructor.js
Created January 29, 2022 08:24
An example of a function constructor
//function constructor
function Employee(empNo,name,department){
this.employeeNumber = empNo
this.name = name;
this.department = department;
}
Employee.prototype.getEmployeeProfile = function(){
return this.name + "|" + this.department + "|" + this.employeeNumber;
@jagmeet-chaudhary
jagmeet-chaudhary / reflection-for-in.js
Created January 28, 2022 12:40
For in loop in javascript (reflection example)
person = {
firstName : "Jack",
lastName : "doresy",
getFullName : function () {
return this.firstName + " " + this.lastName;
}
}
//List down all the properties (including the function)
for(var prop in person){
console.log(prop + "->" + person[prop]);
@jagmeet-chaudhary
jagmeet-chaudhary / functional-programming.js
Created January 28, 2022 11:46
simple functional programming example using array and filter and map functions
function filterFn(arr, predicate) {
resultArr = [];
for (i = 0; i < arr.length; i++) {
if (predicate(arr[i])) {
resultArr.push(arr[i]);
}
}
return resultArr;
}
@jagmeet-chaudhary
jagmeet-chaudhary / call-apply-bind.js
Created January 28, 2022 09:47
Example of call ,apply and bind
function getName(){
console.log(this.firstName + " " + this.lastName);
}
function Add(n1,n2){
console.log(n1 + n2);
}
getName(); // undefined undefined
person1 = {
firstName : "jack",
@jagmeet-chaudhary
jagmeet-chaudhary / closures2.js
Last active January 28, 2022 09:12
Closure example
function createFunctionArray(){
arr = [];
for(i=0;i<3;i++){
let j = i;
arr.push (function(){
console.log("i =" + i); //Always print 3
console.log("j =" + j); //Prints the value of i for each iteration
})
}
@jagmeet-chaudhary
jagmeet-chaudhary / closures.js
Created January 28, 2022 09:05
Simple javascript closure example
function sayGreeting(greeting){
var innerVar = "inner";
return function() {
console.log(greeting + "," + innerVar);
}
}
var greetingFn = sayGreeting("hi");
greetingFn();//prints -> hi,inner
// greetingFn still has access to 'innerVar' and 'greeting' passed since its part of the
@jagmeet-chaudhary
jagmeet-chaudhary / Employee data.json
Created June 30, 2020 02:55
Example demonstrating difference between a stable vs unstable sort.
[{"Name":"Dix","Age":27,"Designation":"Analyst","Paygrade":"Leve1","Department":"Marketing"},
{"Name":"Alphonso","Age":48,"Designation":"Director","Paygrade":"Executive","Department":"Product Management"},
{"Name":"Lester","Age":36,"Designation":"Manager","Paygrade":"Level3","Department":"Services"},
{"Name":"Lennard","Age":30,"Designation":"Director","Paygrade":"Level3","Department":"Legal"},
{"Name":"Bryn","Age":27,"Designation":"Sr Analyst","Paygrade":"Executive","Department":"Product Management"},
{"Name":"Mac","Age":40,"Designation":"Director","Paygrade":"Leve1","Department":"Training"},
{"Name":"Enid","Age":34,"Designation":"Director","Paygrade":"Level3","Department":"Services"},
{"Name":"Nanon","Age":44,"Designation":"Analyst","Paygrade":"Executive","Department":"Engineering"},
{"Name":"Booth","Age":45,"Designation":"Director","Paygrade":"Level3","Department":"Human Resources"},
{"Name":"Elsinore","Age":46,"Designation":"Manager","Paygrade":"Level2","Department":"Product Management"}]