Last active
December 26, 2019 15:13
-
-
Save dhval/6008582 to your computer and use it in GitHub Desktop.
JavaScript inheritance - http://davidshariff.com/blog/javascript-inheritance-patterns/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# check sub-string exists | |
str.indexOf("oo") !== -1 | |
# prototype f() and regex replace | |
String.prototype.trim = function() { | |
return this.replace(/^\s+|\s+$/g, ""); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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