Last active
December 6, 2015 00:17
-
-
Save rolandovillca/1212521292a2c49658bd to your computer and use it in GitHub Desktop.
NODEJS TIPS
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
//****************************************************************** | |
// Steps to set nodejs, expressjs and grunt: | |
// 1. Install nodejs | |
// $ sudo npm install nodejs | |
// 2. Install expressjs//# 3. Install grunt-cli | |
// 5. Create a project using express (building a skeleton directory) | |
// 6. Install grunt | |
// 7. Create Package folder | |
// 8. Run grunt command | |
// 9. Create basic create and clean tasks | |
//****************************************************************** | |
// DEBUGGING DOM VIA PROTRACTOR: | |
// $ node_modules/protractor/bin/elementexplorer.js https://localhost/#/page1 | |
//***************** | |
// CONTEXT vs SCOPE | |
//***************** | |
// Every function invocation has both a scope and a context associated with it. | |
// Fundamentally, scope is function-based while context is object-based. In other words: | |
// * Scope pertains to the variable access of a function when it is invoked and is unique to each invocation. | |
// * Context is always the value of the this keyword which is a reference to the object that “owns” the currently executing code. | |
//********************** | |
// EVAL vs CALL vs APPLY | |
//********************** | |
// Define 2 objects, that will only be called by other functions. | |
var person1 = {name: 'Marvin', age: 42, size: '2xM'} | |
var person2 = {name: 'Zaphod', age: 4200000000, size: '1xS'} | |
// Define 2 methods, that will run into one context of different function. | |
var sayHello = function () { | |
console.log('Hello, ' + this.name + ' -- ' + this.age); | |
}; | |
var sayGoodBye = function () { | |
console.log('GoodBye, ' + this.name); | |
}; | |
// Wwill give an error because: | |
// both functions rely on their scope for the 'this.name' data, | |
// and calling them without explicit scope will just run them in the scope of the current window. | |
sayHello(); | |
sayGoodBye(); | |
// All next 4 lines do exactly the same thing. | |
// The sayHello or sayGoodBye will run in the scope of either person1 or person2. | |
sayHello.call(person1); | |
sayGoodBye.call(person2); | |
sayHello.apply(person1); | |
sayGoodBye.apply(person2); | |
/* | |
* Note: | |
* Both call and apply perform very similar functions: | |
* they execute a function in the context, or scope, of the first argument that you pass to them. | |
* Also, they're both functions that can only be called on other functions. | |
*/ | |
// The eval() function evaluates or executes an argument. | |
// If the argument is an expression, eval() evaluates the expression. | |
// If the argument is one or more JavaScript statements, eval() executes the statements. | |
var x = 10; | |
var y = 20; | |
var a = eval("x * y") + "<br>"; | |
var b = eval("2 + 2") + "<br>"; | |
var c = eval("x + 17") + "<br>"; | |
var res = a + b + c; | |
// The result of res will be: | |
200 | |
4 | |
27 | |
//********************************************************** | |
// SELF EXECUTING ANONYMOUS FUNCTIONS (IIFE or SEAF pattern) | |
//********************************************************** | |
//ExampleS: | |
// Creates a function object | |
var f1 = (function() { alert('foo'); }); | |
// Creates a function object and executes it immediately | |
var f2 = (function() { alert('foo'); })(); | |
// Allows 'alert('foo')' to be executed immediately, as if it was just written inline, | |
// but also within its own scope so as not to affect the global namespace | |
// (and thus potentially interfere with or be interfered with by, other scripts). | |
// The difference is that f1 gives you a function object. f2 creates and invokes an anonymous function. | |
// By using an anonymous function in this manner you ensure it's only executed once. | |
// Example: | |
var counter = (function(){ | |
var i = 0; | |
return { | |
get: function(){ | |
return i; | |
}, | |
set: function( val ){ | |
i = val; | |
}, | |
increment: function() { | |
return ++i; | |
} | |
}; | |
}()); | |
// 'counter' is an object with properties, which in this case happen to be methods. | |
counter.get(); // 0 | |
counter.set( 3 ); | |
counter.increment(); // 4 | |
counter.increment(); // 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment