Skip to content

Instantly share code, notes, and snippets.

@jfairbank
Forked from mjc-gh/example_1.js
Last active September 26, 2017 16:18
Show Gist options
  • Save jfairbank/3679aa2d03d4165a6a1988d327e6e171 to your computer and use it in GitHub Desktop.
Save jfairbank/3679aa2d03d4165a6a1988d327e6e171 to your computer and use it in GitHub Desktop.
JavaScript Code Examples
// Question: Explain the difference in how x is defined in these three examples
// Sample A
function a(){
x = 1;
}
// Sample B
function b(){
var x = 1;
}
// Sample C
x = 2;
function c(){
var x = 1;
}
// Question: In what order will the strings be printed to the console when this file is executed?
// Assume fetch is a function that requests a URL and returns a Promise
function fetchData() {
return fetch('/data').then(function() {
console.log('a');
});
}
console.log('c');
fetchData().then(function() {
console.log('d');
});
console.log('b');
// User Constructor
function User(){
this.getName = function(){};
}
// Task Constructor
function Task(){}
Task.prototype.getSummary = function(){};
var user = new User();
var task = new Task();
// Question: What is the result of these console statements? Why is this the case?
console.log(user.hasOwnProperty('getName'));
console.log(task.hasOwnProperty('getSummary'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment