/**/
Understanding "this" in JavaScript
/**/
4 ways that "this" takes a value:
/****/ First way /****/
function helloWorld() { console.log("Hello world!"); };
//"this"in a browser, it's the window // bit different in node.js
/****/ Second way /****/
//this is done using Node
var Portland = { bridges: 12, airport: 1, soccerTeams: 1, logNumberOfBridges: function() { console.log("There are " + this.bridges + " bridges in Portland!") }, logTeams: function() { console.log(this.soccerTeams); } }
//another way of logteams using this function logTeams() { console.log(this.soccerTeams); //if the word was just this, it would print all node info }
Portland.foo = logTeams;
Portland.logNumberOfBridges(); Portland.logTeams(); Portland.foo(); logTeams(); //comes back as undefined
/****/ Third way /****/
var City = function(name, state) { this.name = name || 'Portland'; this.state = state || 'Oregon'; this.printMyCityAndState = function() { console.log("My city is " + this.name + ", and my state is " + this.state); }; };
portland = new City(); seattle = new City('Seattle', 'Washington');
console.log(portland); console.log(seattle);
portland.printMyCityAndState(); seattle.printMyCityAndState();