Skip to content

Instantly share code, notes, and snippets.

View kangax's full-sized avatar

Juriy Zaytsev kangax

View GitHub Profile
/* can you spot what is happening here */
/* the alert("X") should never execute */
<script>
onload = function() {
var fn = function() {
alert("X");
}
(function() { })();
/*
Checks that `toString` of constructor's prototype has "special" behavior (i.e. is `Object.prototype.toString`) as per 15.2.4.2
Cons:
- Relies on `constructor` property of an object (as well as `toString` of constructor's prototype) being present and not augmented
(will fail on host objects in IE that lack constructor, but could be guarded against)
- Does not differentiate between Object objects created implicitly via function constructor and those created explicitly via Object
Pros:
/**
* Example:
* 'hello {{username}}'.between('{{','}}') === 'username'
*/
String.prototype.between_regexp = function between(begin, end) {
var regexp = new RegExp(begin + '(.+)' + end);
return this.match(regexp)[1];
}
@kangax
kangax / gist:247885
Created December 3, 2009 04:25 — forked from lsmith/gist:247884
<script>
/* Alternate snippet to asynchronously embed the Google Analytics script.
* This requires the following things of the pages that will contain it:
* 1. the page only include one GA tracker (common case),
* 2. the page is not be behind SSL (http, not https),
* 3. this script block can be in the <head> or <body> elements
*/
var _gaq = [['_setAccount', 'UA-XXXXXX-X'],['_trackPageview']];
(function(d) {
We couldn’t find that file to show.
/* MODIFY PROTOTYPE */
var _fn=function(){};
_fn.prototype.a=1;
var v=new _fn();
alert(v.a === 1); /* true */
alert(v.constructor === _fn); /* true */
/* REPLACE PROTOTYPE */
/**
* Aliases and additional "sugar"
* 2010-04-15
* @author Dmitry A. Soshnikov
*/
(function () {
["defineProperty", "defineProperties", "keys", "getOwnPropertyNames",
"getOwnPropertyDescriptor", "getPrototypeOf", "seal", "freeze",
"preventExtensions", "isFrozen", "isSealed", "isExtensible"]
// @kangax fix yur SubArray (ES5 flavor) kthxbai
// http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/#ecmascript_5_accessors_to_the_rescue

// (C) Juriy Zaytsev (aka @kangax) - MIT Style License
// http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/

var SubArray = (function() {

  var MAX_SIGNED_INT_VALUE = Math.pow(2, 32) - 1,

hasOwnProperty = {}.hasOwnProperty,

var query = (function() {
var query, engine, engines = ('NW.Dom.select,base2.dom.querySelectorAll,Sizzle,$$,$,jQuery,'+
'YAHOO.util.Selector.query,dojo.query,Ext.DomQuery.select,document.querySelectorAll').split(',');
while (engine = engines.shift()) {
if (Function('try{ var els = ' + engine + '("*"); return els.length > 0 }catch(e){}')()) {
query = Function('cssExpr', 'return ' + engine + '(' +
(/^base2/.test(engine) ? 'document,' : '') + 'cssExpr)');
break;
}
/**
* Provides requestAnimationFrame in a cross browser way.
* @author greggman / http://greggman.com/
*/
if (!window.requestAnimationFrame) {
window.requestAnimationFrame =
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||