Skip to content

Instantly share code, notes, and snippets.

View schnogz's full-sized avatar

Andrew Schneider schnogz

View GitHub Profile
@schnogz
schnogz / count-watchers-angularjs.js
Last active August 29, 2015 14:20
Count All Watchers Currently Running on an AngularJS Page
(function () {
// Change root to point at your ng-app in HTML
var root = $(document.getElementsByTagName('body'));
var watchers = [];
var f = function (element) {
if (element.data().hasOwnProperty('$scope')) {
angular.forEach(element.data().$scope.$$watchers,
function (watcher) {
watchers.push(watcher);
@schnogz
schnogz / html5-download-attr.html
Last active August 29, 2015 14:20
Examples of HTML5's Download Attribute
<!-- must always supply an href to file -->
<a href="/file/somethingreallylongandobscure234211.pdf" download>
<!-- if a value is passed to download attr, it will be the name of the file when downloaded -->
<a href="/file/generated-budget-1293.pdf" download="Weekly Budget Recap">
@schnogz
schnogz / javascript-global-scope.js
Created May 14, 2015 03:58
Examples of JavaScript global scoping.
//globals
var x = 4;
function Dog() {
alert("Woof");
}
// 3rd party libraries use the global scope
// making them accessible from anywhere an app
jQuery("#myDiv").empty();
@schnogz
schnogz / isArray-implementation-attemps.js
Last active December 27, 2023 08:13
JavaScript .isArray Implementations
// Attempt #1: using typeof
// fails in all cases since typeof [] returns "object"
Array.prototype.isArray = function(obj) {
return (typeof obj === "array");
}
// Attempt #2: using instanceof
// fails when obj = Array.prototype
// and when array is defined in another window or frame
Array.prototype.isArray = function(obj) {
@schnogz
schnogz / duck-typing-example.js
Created May 17, 2015 18:10
JavaScript Duck Typing Example
function Animal(hasFeathers, sound) {
this.hasFeathers = hasFeathers;
this.sound = sound;
}
Animal.prototype.isDuck = function() {
if (this.hasFeathers && this.sound === "Quack") {
console.log("I am a duck");
} else {
console.log("I am not a duck");
}
@schnogz
schnogz / backbone-widget-view.js
Last active August 29, 2015 14:21
Spying on backbone.js view methods with Sinon.js
define([
'text!./Templates/mainTemplate.html',
'text!./Templates/messageTemplate.html'
], function (
maintemplate,
messageTemplate
) {
'use strict';
@schnogz
schnogz / always-use-semicolons-example1.js
Last active August 29, 2015 14:23
Why semicolons should be used in JavaScript
// The following results in a syntax error.
var a = 'a'
[].forEach.call(document.querySelectorAll('.md'), function(e) {
console.log(e);
});
// Adding the semicolon and now everything works just fine.
var a = 'a';
[].forEach.call(document.querySelectorAll('.md'), function(e) {
console.log(e);
@schnogz
schnogz / humorous-code-comments.js
Last active November 16, 2024 11:17
Top 15: Best source code comments
======================================================
// The magnitude of this hack compares favorably with that of the US national debt.
======================================================
// Warning: Thar be dragons lurkin and black magic at work here.
// Modify at your own risk!
//
// ^ ^
@schnogz
schnogz / basic-constructor-function-mixed.js
Last active August 29, 2015 14:25
methods within constructor vs prototype in javascript
function Dog (name, breed) {
// instance variables
this.name = name;
this.breed = breed;
var medicalRecords = {
"hasWorms": true,
"hasDiabetes": false
};
@schnogz
schnogz / jquery-example.js
Last active October 28, 2015 20:34
monkey patching
// create a closure and remap jQuery to $
(function($){
// save off original method
var _originalAppendTo = $.fn.appendTo;
// override method
$.fn.appendTo = function() {
// silly code to alternate backgound color
if ($(document.body).children().length % 2) {
document.body.style.background = "#f285cf";