Skip to content

Instantly share code, notes, and snippets.

@dhval
Last active December 26, 2019 15:13
Show Gist options
  • Save dhval/6008582 to your computer and use it in GitHub Desktop.
Save dhval/6008582 to your computer and use it in GitHub Desktop.

Various js sniplets

# check sub-string exists
str.indexOf("oo") !== -1
# prototype f() and regex replace
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, "");
};
// JavaScript inheritance - http://davidshariff.com/blog/javascript-inheritance-patterns/
/**
* 1. Pseudoclassical pattern, similar to classical programming languages.
**/
var extendObj = function(childObj, parentObj) {
var tmpObj = function () {}
tmpObj.prototype = parentObj.prototype;
// fix pass by reference, any changes to child.prototype won't effect parent.prototype ( and other siblings)
childObj.prototype = new tmpObj();
// reset the constructor
childObj.prototype.constructor = childObj;
};
/**
* 2. Douglas Crockford, The good parts - Functional pattern
* Create a new constructor function, whose prototype is the parent object's prototype.
**/
var vehicle = function(attrs) {
var _privateObj = {
hasEngine: true
},
that = {};
that.name = attrs.name || null;
that.hasEngine = function () {
alert('This ' + that.name + ' has an engine: ' + _privateObj.hasEngine);
};
return that;
}
var motorbike = function () {
// private
var _privateObj = {
numWheels: 2
},
// inherit
that = vehicle({
name: 'Motorbike',
engineSize: 'Small'
});
// public
that.totalNumWheels = function () {
alert('This Motobike has ' + _privateObj.numWheels + ' wheels');
};
return that;
};
/**
* 3. Object.create as of ECMAScript 5
**/
var child = Object.create(parent, {
gender : {value: 'Male'}
});
# http://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript
var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
alert(headers);
/**
* http://www.stevesouders.com/blog/2010/05/11/appendchild-vs-insertbefore/
* http://www.dustindiaz.com/scriptjs
* http://unixpapa.com/js/dyna.html
* http://stackoverflow.com/questions/950087/how-to-include-a-javascript-file-in-another-javascript-file
**/
javascript:(function(){
var scriptEl = document.createElement('script');
scriptEl.async = 'true';
scriptEl.type = 'text/javascript';
scriptEl.src='https://code.jquery.com/jquery-1.4.2.js';
var ref = document.getElementsByTagName('script')[0];
ref.parentNode.insertBefore(scriptEl, ref);
})()
/** In the main window: **/
var popupWin = null;
function openPopup() {
var url = "popup.htm";
popupWin = open( "", "popupWin", "width=500,height=400" );
if( !popupWin || popupWin.closed || !popupWin.doSomething ) {
popupWin = window.open( url, "popupWin", "width=500,height=400" );
} else popupWin.focus();
}
function doSomething() {
openPopup();
popupWin.doSomething();
}
/** In the popup: **/
self.focus();
function doSomething() {
alert("I'm doing something");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment