Skip to content

Instantly share code, notes, and snippets.

@eshacker
eshacker / apply_and_call.js
Created February 1, 2016 09:00
Apply and Call in JavaScript
/* when there is no difference in apply and call */
// 1.
var func = function(arr, barr, carr){
this.arr = arr;
console.log(typeof(arr), arr.length, arr, barr, carr);
};
func.call(null, 1, 2, 3); // number undefined 1 2 3
@eshacker
eshacker / bind.js
Created February 1, 2016 08:43
A short tutorial on bind
var x = 1;
var A = {
x : 10,
get: function(){ return this.x; }
};
var a = A.get;
console.log(a()); // 1
@eshacker
eshacker / short_this_js.js
Created February 1, 2016 06:15
A very short study in this of JavaScript
function Namer(){
console.log(this instanceof Namer);
}
Namer(); // false
new Namer(); // true
Namer.call(Namer.prototype); // false
Namer.call(new Namer); // true true
Namer.call(Namer); // false
Namer.call(Object.create(Namer.prototype)); // true
@eshacker
eshacker / gcd.cs
Created January 28, 2016 10:15
Finding GCD recursively
using System;
class Solution {
static void Main(String[] args) {
var numbers = Console.ReadLine().Split(' ');
var x = int.Parse(numbers[0]);
var y = int.Parse(numbers[1]);
Console.WriteLine(gcd(x, y));
}
static int gcd(int x, int y){
@eshacker
eshacker / split_check.js
Created January 26, 2016 12:53
Quick split() check
console.log("1,2,3,4".split(''))
["1", ",", "2", ",", "3", ",", "4"]
console.log("1,2,3,4".split(','))
["1", "2", "3", "4"]
console.log("1,2,3,4".split())
["1,2,3,4"]
console.log("1 2 3 4".split())
@eshacker
eshacker / convert.js
Created January 26, 2016 11:00
Converting comman man time to military time
function convertPM(time){
if(time.startsWith('12')){
return time.replace('PM','');
}
return (parseInt(time.split(':'))+12) + time.substring(2, time.length-2);
}
function convertAM(time){
if(time.startsWith('12')){
return '00'+time.substring(2, time.length-2);
@eshacker
eshacker / Babel_output_Explaining classes in ES 2015 via Man and SuperMan.js
Created December 29, 2015 14:33
Explaining classes in ES 2015 via Man and SuperMan
"use strict";
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); }
@eshacker
eshacker / babel_output_arrow_function_in_es_5.js
Created December 29, 2015 14:13
Arrow functions and lexical this
"use strict";
var Person = function Person(name, age, sex, friends) {
var self = this !== undefined ? this : Object.create(Person.prototype);
self.Name = name;
self.Age = age;
self.Sex = sex;
self.Friends = friends;
self.SayHelloToMyFriends = function () {
var _this = this;
@eshacker
eshacker / arrow_functions_usage_on_arrays.js
Created December 29, 2015 13:31
Using arrow functions to generate arrays out of original array.
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log("arr: ", arr);
let even = arr.filter(x => x%2 === 0);
console.log("evens: ", even);
let odd = arr.filter(x => x%2 === 1);
console.log("odds: ", odd);
let gt5 = arr.filter(x => x > 5);
@eshacker
eshacker / destructuring_in_es_2015.js
Created December 29, 2015 13:19
Destructuring in ES 2015
// ES 2015
var DefaultPerson = {
name : { first: "John", last: "Smith"},
age: 25,
city: { hometown: "NY", current: "PA" }
};
var Person = {
name : { first: "EcmaScript", last: "Hacker"},
age: 17,