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
| // Set up a simple object to use as "context" | |
| var context = { foo: "bar" }; | |
| // A function that uses a reference to a variable called "foo" | |
| // on the "this" context. | |
| function returnFoo () { | |
| return this.foo; | |
| } | |
| // This variable does not exist on scope, so is undefined. |
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 sleep(milliSeconds) { | |
| var startTime = new Date().getTime(); | |
| while (new Date().getTime() < startTime + milliSeconds); | |
| } | |
| sleep(10000); |
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 getterSetter = function() { | |
| var items = {}; | |
| this.get = function(name) { | |
| return items[name]; | |
| }; | |
| this.set = function(name, value) { | |
| items[name] = 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 MongoClient = require('mongodb').MongoClient, | |
| request = require('request'); | |
| MongoClient.connect('mongodb://localhost:27017/course', function(err, db) { | |
| if(err) throw err; | |
| request('http://www.reddit.com/r/technology/.json', function(error, response, body) { | |
| if(!error && response.statusCode == 200) { | |
| var obj = JSON.parse(body); | |
| var stories = obj.data.children.map(function(story){ return story.data; }); |
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
| // Mistake | |
| var str = "David is an Arsenal fan, which means David is great"; | |
| str.replace("David", "Darren"); // "Darren is an Arsenal fan, which means David is great" | |
| // Desired | |
| str.replace(/David/g, "Darren"); // "Darren is an Arsenal fan, which means Darren is great" |
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
| [ | |
| { name: "Robin Van PurseStrings", age: 30 }, | |
| { name: "Theo Walcott", age: 24 }, | |
| { name: "Bacary Sagna", age: 28 } | |
| ].sort(function(obj1, obj2) { | |
| // Ascending: first age less than the previous | |
| return obj1.age - obj2.age; | |
| }); | |
| // Returns: | |
| // [ |
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 mergeTo = [4,5,6], | |
| var mergeFrom = [7,8,9]; | |
| Array.prototype.push.apply(mergeTo, mergeFrom); | |
| mergeTo; // is: [4, 5, 6, 7, 8, 9] |
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 howManyStringsInTheHouse(fullText, searchText){ | |
| var regx = new RegExp(searchText,"g"), | |
| testResults = fullText.match(regx); | |
| if (testResults) { | |
| return testResults.length; | |
| } | |
| else | |
| return "Nothing here man"; | |
| } |
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
| // Boring | |
| if (success) { | |
| obj.start(); | |
| } else { | |
| obj.stop(); | |
| } | |
| // Hipster-fun | |
| var method = (success ? 'start' : 'stop'); | |
| obj[method](); |
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
| ['first', 'name'].join(' '); // = 'first name'; | |
| ['milk', 'coffee', 'suger'].join(', '); // = 'milk, coffee, suger' |