Skip to content

Instantly share code, notes, and snippets.

View z1yuan's full-sized avatar

Zhi Yuan z1yuan

  • Hangzhou China
View GitHub Profile
@eswak
eswak / mochachaisinon.js
Last active April 16, 2019 14:21
An example of asynchronous JavaScript testing using Mocha + Chai + Sinon
var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;
var should = chai.should();
var sinon = require('sinon');
function myAsyncFunction(callback) {
// 50ms delay before callback
setTimeout(function() {
console.log('hello');
@cyrus-and
cyrus-and / this.js
Last active July 9, 2019 11:50
JavaScript `this` examples from http://stackoverflow.com/a/12371105/477168
// JavaScript `this` examples from http://stackoverflow.com/a/12371105/477168
// The value of this in a function is sometimes called the "context" in which
// the function runs. The most important thing to understand is that a function
// object does not have a fixed context -- the value of this changes depending
// on how the function is called.
// If the function is called as a "raw" function (e.g., just do someFunc()),
// this will be window (or undefined if the function runs in strict mode).