This file contains hidden or 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
//#JS #bookmarkelt | |
//redirect to https | |
(function() { | |
var url = window.location.href.toLowerCase(); | |
if(url.indexOf('https:') < 0 && url.indexOf('http:') >= 0) { | |
var httpsUrl = 'https:' + window.location.href.substring(5); //url.replace('http', 'https'); | |
window.location.href = httpsUrl; | |
} | |
})() |
This file contains hidden or 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
var isArray = function (value) { | |
return value && | |
typeof value === 'object' && | |
typeof value.length === 'number' && | |
typeof value.splice === 'function' && | |
!(value.propertyIsEnumerable('length')); | |
}; |
This file contains hidden or 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
var isNumber = function isNumber(value) { | |
return typeof value === 'number' && isFinite(value); | |
} |
This file contains hidden or 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
var mammal = function (spec) { | |
var that = {}; | |
that.get_name = function ( ) { | |
return spec.name; | |
}; | |
that.says = function ( ) { | |
return spec.saying || ''; | |
}; | |
return that; | |
}; |
This file contains hidden or 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 lacks a method that removes spaces from the ends of a string. | |
//That is an easy oversight to fix: | |
String.method('trim', function ( ) { | |
return this.replace(/^\s+|\s+$/g, ''); | |
}); | |
document.writeln('"' + " neat ".trim( ) + '"'); |
This file contains hidden or 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
//returns only integer parts | |
Number.method('integer', function ( ) { | |
return Math[this < 0 ? 'ceiling' : 'floor'](this); | |
}); | |
document.writeln((-10 / 3).integer( )); // -3 |
This file contains hidden or 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
var serial_maker = function () { | |
// Produce an object that produces unique strings. A | |
// unique string is made up of two parts: a prefix | |
// and a sequence number. The object comes with | |
// methods for setting the prefix and sequence | |
// number, and a gensym method that produces unique | |
// strings. | |
var prefix = ''; | |
var seq = 0; |
This file contains hidden or 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
//Instead of initializing myObject with an object literal, we will initialize myObject by | |
//calling a function that returns an object literal. That function defines a value variable. That variable is always available to the increment and getValue methods, but | |
//the function’s scope keeps it hidden from the rest of the program. | |
//We are not assigning a function to myObject. We are assigning the result of invoking | |
//that function. Notice the()on the last line. The function returns an object containing two methods, and those methods continue to enjoy the privilege of access to the | |
//value variable. | |
var myObject = function ( ) { | |
var value = 0; | |
return { | |
increment: function (inc) { |
This file contains hidden or 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
Function.prototype.method = function (name, func) { | |
this.prototype[name] = func; | |
return this; | |
}; | |
// Add a method conditionally. | |
Function.prototype.method = function (name, func) { | |
if (!this.prototype[name]) { | |
this.prototype[name] = func; | |
} |
This file contains hidden or 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
var add = function (a, b) { | |
if (typeof a !== 'number' || typeof b !== 'number') { | |
throw { | |
name: 'TypeError', | |
message: 'add needs numbers' | |
}; | |
} | |
return a + b; | |
} |