* @author Piyush Arya
* Module :- UnderScore.js Tutorial
* Date Created :- Jan 2014
*/
var contactList = {
"returnList" : [{
"name" : "NAME",
"password" : "dummy",
"time" : "today",
"email" : "a.com"
}],
};
window.logger = function(param) {
return console.log(param);
};
var arr = ["1", "2", "3", "4", "5"], every = [true, 0, "hello"], invokeSort = [[3, 6, 5, 2, 4, 1],
["b", "d", "c", "a"]], invokeupper = ["b", "d", "c", "a"], arrColl = [1, 2, 3, 4, 5],
json = [{
"name" : "piyush",
"email" : "demo@gmail.com",
"age" : 25
}, {
"name" : "piyush",
"email" : "demo@yahoo.com",
"age" : 26
}, {
"name" : "arya",
"email" : "demo@hotmail.com",
"age" : 27
}];
/* **** UNCOMMENT LOGGER TO SEE THE RESULT IN CONSOLE***** */
/* ******************FUNCTIONS EXAMPLES ************************* */
/* ---------------------------bind Function Starts----------------------------------------- */
var o = {
greets : "Welcome"
}, f = function(name) {
return this.greets + " " + name;
};
var call = _.bind(f, o);
//logger(call("People"));
//The bind functions basically lets you keep the value of this whenever and wherever the function is called.
//This is specially useful when you’re working with event handlers where this is hijacked.
/* -----------------------bind Function end---------------------------------------------------- */
/* ------------------------bindAll Function Starts---------------------------------------------- */
var buttonView = {
label : 'Action Performed',
onClick : function() {
console.log('clicked: ' + this.label);
},
onmouseover : function() {
console.log('hovering: ' + this.label);
}
};
_.bindAll(buttonView, 'onClick', 'onmouseover');
// Bind methods
jQuery('.contact').bind('mouseout', buttonView.onmouseover);
// You can use n no functions into it.
/* --------------------------bindAll Function end------------------------------------------------ */
/* ---------------------------partial Function Starts-------------------------------------------- */
var add = function(a, b) {
return a * b;
};
add5 = _.partial(add, 5);
//logger(add5(10));
//_.partial(function, [*arguments])
// first parameter in _.partial will be the function and second parameter value.
// Once first value is passed with function it will be constant
/* --------------------------partial Function end------------------------------------------------ */
/* --------------------------delay Function Starts---------------------------------------------- */
var binding = _.bind(console.log);
//_.delay(binding, 1000, 'Delay');
// _.delay(function, wait, [*arguments])
// Will delay the function
/* --------------------------delay Function end----------------------------------------------- */
/* --------------------------defer Function Starts--------------------------------------------- */
_.defer(function() {
// console.log("This will execute when idle.");
});
/*The special utility function defer executes a function in a non-blocking */
/*fashion for the UI thread, waiting for the call stack to clear. */
/* --------------------------defer Function end---------------------------------------------------- */
/* --------------------------once Function Starts---------------------------------------------------- */
//var initializeOnce = _.once(createApplication);
//initializeOnce();
//initializeOnce();
// Application is only created once.
/* --------------------------once Function end---------------------------------------------------- */
/* --------------------------after Function Start---------------------------------------------------- */
//_.after(count, function)
//var renderNotes = _.after(notes.length, render);
//_.each(notes, function(note) {
// note.asyncSave({success: renderNotes});
//});
// renderNotes is run once, after all notes have saved.
/* --------------------------------------after Function end---------------------------------------- */
/* --------------------------------------wrap Function Start--------------------------------------- */
var wraps = function(name) {
return "hello: " + name;
};
wraps = _.wrap(wraps, function(func) {
return "before, " + func("People") + ", after";
});
//logger(wraps());
// called function inside a function. Wrappped
/* --------------------------------------wrap Function end------------------------------------ */
/* --------------------------------------compose Function Start--------------------------------- */
var greeting = function(name) {
return "Welcome: " + name;
};
var override = function(statement) {
return statement.toUpperCase() + "!";
};
var welcome = _.compose(greeting, override);
//logger(welcome('People'));
/*each function consumes the return value of the function that follows. In math terms,
composing the functions f(), g(), and h() produces f(g(h())).*/
/* ------------------------------compose Function end----------------------------- */
Click Here For Underscore Collections Functions with Examples
No comments:
Post a Comment