Skip to content

Instantly share code, notes, and snippets.

@geastwood
geastwood / useStrictMode.md
Last active August 29, 2015 14:03
strict mode
  • this will not refer to window object
  • callee, caller of arguments object are not allowed under strict mode
  • under strict mode parameter is not refereced linked to arguments object, no strict mode, parameter is a reference of the arguments object.
(function() {
    var nostrict = function(x) {
        arguments[0] = 'modified';
        console.log('arguments[0] =', arguments[0]);
        console.log('x=', arguments[0]);
 console.log('under non-strict mode,', x === arguments[0]); // log true
@geastwood
geastwood / named_function.js
Created June 26, 2014 07:17
named function
var constructor = function() {return null;};
var f = function f() {
return constructor();
};
f();
// Under ES3
// logs {}, object literal
// Under ES5

Generate take-away

Concept 1: Iterators

In computer programming, an iterator is an object that enables a programmer to traverse a container.

When invoked, generator functions return iterators

Concept 2: Run-to-completion

Run-to-completion scheduling is a scheduling model in which each task runs until it either finishes, or explicitly yields control back to the scheduler.

/*
* Mongoose magic
*/
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/fei');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function() {
var kittySchema = mongoose.Schema({
name: String
define(function() {
var ScriptLoader = function(url, ns, onLoadCallback, name) {
this.url = url; // url of the module
this.ns = ns; // source obj/ns obj to bind 'define' method
this.onLoadCallback = onLoadCallback;
this.name = name; // dependency name
this.load();
};

Apache

server

/etc/init.d/apache2 start/stop/restart

folder structure

/etc/apache2/ // main folder
# Apache
```
/etc/init.d/apache2 start/stop/restart
```
@geastwood
geastwood / vim_cheat_sheet.md
Last active December 23, 2016 18:56
vim, cheatsheet

Vim CHEAT SHEET

Folding

Keystrokes Description
zc Close a fold
zo Open a fold
za Toggle a fold
zR Open all folds

pattern to replace setInterval with setTimeout

Register a task with setInterval and a interval is not desirable, since the task can take more time than the interval, so the compiler will skip through the current cycle. Replace setInterval with setTimeout with following pattern:

setInterval(function() {
    runSomething();
}, interval);

with

(function loop() {

Why passing undefined to Immediately invoking function

(function(window, document, undefined) {...})(window, document);
  • save undefined variable from redeclearation
  • save bit from minification, since the variables will be replaced with shorter alias.