Skip to content

Instantly share code, notes, and snippets.

View krazylearner's full-sized avatar
🏠
Working from home

Ankur Bansal krazylearner

🏠
Working from home
View GitHub Profile
// Pattern
//Usually, you call a callback as the last thing you do inside a function.
//You might consider it synonymous with return, only JavaScript does
//not halt function execution when it hits it. It can be easy to accidentally
//let execution continue after calling a callback, when you really expected it to end.
//In order to make this easy to spot, and make sure execution stops in the way you expect,
//I recommend returning the callback function call.
// wrong!
/*
What is "this"?
In all programming languages, there is this idea of current scope and current context. In JavaScript we have a lexical scope and a current "this" context.
In JavaScript all new scopes are created through "function" definitions. But contrary to other c-like languages, this is the only way to make a new scope. For loops don't do it, if blocks don't do it, plain curly braces assuredly don't do it. This simplicity is both a blessing and a curse. First let's have a couple of examples to explain creating scopes.
This is an example of global scope:
*/
WebSockets vs. Server-Sent events/EventSource
---------------------------------------------
Both WebSockets and Server-Sent Events are capable of pushing data to browsers. To me they seem to be competing technologies.
What is the difference between them? When would you choose one over the other?
Websockets and SSE (Server Sent Events) are both capable of pushing data to browsers, however they are not competing technologies.
Websockets connections can both send data to the browser and receive data from the browser.
A good example of an application that could use websockets is a chat application.
@krazylearner
krazylearner / how-to-module
Last active August 29, 2015 14:19
These are some basic steps for writing a NodeJS module. Most of the suggestions in this document are optional. . This is a set of patterns that noders have found to work for them and their projects.
##Use Git
Most people in the node community use git for all their version control needs. It is an extremely powerful and robust tool. If you don't already use it, you should.
Don't wait until your program is "ready" before using git on it! Run git init in the root of your project folder right away, and commit your changes as you make them. It is a good habit, and can help avoid a lot of painful mishaps.
If you wish to share your program with others, then github is also a tremendously useful resource that most nodejs developers use.
A package.json File
=========================
@krazylearner
krazylearner / Reading_writing_text_files
Created April 14, 2015 12:35
Reading and writing text files
In JDK 7, the most important classes for text files are:
Paths and Path - file locations/names, but not their content.
Files - operations on file content.
StandardCharsets and Charset (an older class), for encodings of text files.
the File.toPath method, which lets older code interact nicely with the newer java.nio API.
In addition, the following classes are also commonly used with text files, for both JDK 7 and earlier versions:
Scanner - allows reading files in a compact way
BufferedReader - readLine
BufferedWriter - write + newLine
When reading and writing text files:
@krazylearner
krazylearner / serverApp
Created April 25, 2015 09:46
good implementation of server in nodejs
"use strict";
var http = require("http");
var https = require("https");
/**
* The default port that node servers bind to.
*/
var DEFAULT_PORT = 5000;
@krazylearner
krazylearner / cookie generator
Last active August 29, 2015 14:20
/** * Creates a cookie string using the given options, which may be any of * the following: * * - value * - domain * - path * - expires * - secure * - httpOnly or HttpOnly */
@krazylearner
krazylearner / javascript_Object.defineProperty
Created April 28, 2015 11:24
how to use javascript Object.defineProperty
//Since you asked a similar question, let's take it step by step.
//It's a bit longer, but it may save you much more time than I have spent on writing this:
//Property is an OOP feature designed for clean separation of client code.
//For example, in some e-shop you might have objects like this:
function Product(name,price) {
this.name = name;
this.price = price;
this.discount = 0;
@krazylearner
krazylearner / Normalizes-HTTP-header-names
Created May 7, 2015 12:31
Normalizes HTTP header names according to RFC 2616.
/**
* A map of HTTP header names with irregular case.
* IrregularHeaderNames.js
*/
module.exports = [
'Content-ID',
'Content-MD5',
'DNT',
'ETag',
'P3P',
@krazylearner
krazylearner / Filesystem_structure_Python_project
Created June 11, 2015 12:08
Filesystem structure of a Python project
Do:
================
1.name the directory something related to your project. For example, if your project is named "Twisted",
name the top-level directory for its source files Twisted. When you do releases, you should include a version number suffix: Twisted-2.5.
2.create a directory Twisted/bin and put your executables there, if you have any.
Don't give them a .py extension, even if they are Python source files.
Don't put any code in them except an import of and call to a main function defined somewhere else in your projects.
(Slight wrinkle: since on Windows, the interpreter is selected by the file extension,
your Windows users actually do want the .py extension.
So, when you package for Windows, you may want to add it.