var now = new Date();
now.format("m/dd/yy");
// Returns, e.g., 6/09/07
// Can also be used as a standalone function
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM
// You can use one of several named masks
now.format("isoDateTime");
// 2007-06-09T17:46:21
// ...Or add your own
dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"';
now.format("hammerTime");
// 17:46! Can't touch this!
// When using the standalone dateFormat function,
// you can also provide the date as a string
dateFormat("Jun 9 2007", "fullDate");
// Saturday, June 9, 2007
// Note that if you don't include the mask argument,
// dateFormat.masks.default is used
now.format();
// Sat Jun 09 2007 17:46:21
// And if you don't include the date argument,
// the current date and time is used
dateFormat();
// Sat Jun 09 2007 17:46:22
// You can also skip the date argument (as long as your mask doesn't
// contain any numbers), in which case the current date/time is used
dateFormat("longTime");
// 5:46:22 PM EST
// And finally, you can convert local time to UTC time. Either pass in
// true as an additional argument (no argument skipping allowed in this case):
dateFormat(now, "longTime", true);
now.format("longTime", true);
// Both lines return, e.g., 10:46:21 PM UTC
// ...Or add the prefix "UTC:" to your mask.
now.format("UTC:h:MM:ss TT Z");
// 10:46:21 PM UTCNeeded to make an array of functions, but certain parameter has to vary inside the function:
// The array containing the elements each for every functon
var comments = ['1', '2', '3', '4', '5']
// The function with the different element
function saveComment (callback, i) {
console.log(comments[i]);
callback();
}
// Wrapper, (or `currier`)
function fn (i) {
return function (callback) {
return saveComment (callback, i);
};
};
// The array containing the functions
var myArray = [];
// The loop to compose them
for (var j = 0; j < 5; j ++) {
var saveCommentForI = fn(j);
myArray.push(saveCommentForI);
};
// Executing our array of functions
for (var k = 0; k < 5; k++) {
myArray[k](function(){
console.log('callback invoked')
});
};
/**
Result should be:
$ node test.js
1
callback invoked
2
callback invoked
3
callback invoked
4
callback invoked
5
callback invoked
*/
function containerScope(){
var a = [];
var b = {};
var c = true;
var d = 22;
function argumentsObject(a, b, c, d){
console.log(arguments.length);
console.log(arguments[0]);//Array
console.log(arguments[1]);//Object
console.log(arguments[2]);//true
console.log(arguments[3]);//22
arguments[3] = 55;//I have now changed d as well!
console.log(arguments[3]);
console.log(d);
}
argumentsObject(a, b, c, d);
}
via SNIPPLR
This is the workaround:
// For IE8 and earlier version.
if (!Date.now) {
Date.now = function() {
return new Date().valueOf();
}
}
source: Here
If you want to check if a variable exist
if (typeof yourvar != 'undefined') // Any scope
if (window['varname'] != undefined) // Global scope
if (window['varname'] != void 0) // Old browsers
Add an empty string to your number:
> my_number = 323 > my_array = my_number+'' > typeof(my_array) string
> typeof(1974689+'') string
> my_number = 159627221 > typeof(my_number) number > my_number = my_number + '' 159627221 > typeof(my_number) string
That’s going straight to the KISS awards
via javascripter.net
NoFlo - Managing workflows with JavaScript. My presentation from the Boston JS.Everywhere conference.
The slides are also available.
As part of our efforts to make list creation a simple experience, we have provided an auto suggest on certain fields when creating and remixing a Top10 since day one.
We initially tried several third party packages, but soon found out that none fitted with our expectations in either speed…
Asynchronous patterns are becoming more common and more important to moving web programming forward. They can be challenging to work with in JavaScript. To make asynchronous (or async) patterns easier, JavaScript libraries (like jQuery and Dojo) have added an abstraction called promises (or sometimes deferreds). With these libraries, developers can use promises in any browser with good ECMAScript 5 support. In this post, we’ll explore how to use promises in your web applications using XMLHttpRequest2 (XHR2) as a specific example. Benefits and Challenges with Asynchronous Programming As an example, consider a web page that starts an asynchronous operation like XMLHttpRequest2 (XHR2) or Web Workers. There’s a benefit as some work happens “in parallel.” There’s complexity for the developer to keep the page responsive to people and not block human interaction while coordinating what the web page is doing with the asynchronous work. There’s complexity because program execution no longer really follows a simple linear path.