Last active
May 26, 2017 11:05
-
-
Save beshanoe/7afd18480510b15108d56d931f9491d8 to your computer and use it in GitHub Desktop.
ES2015 Interview Questions
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
// 1 | |
var foo = 2 | |
function bar() { | |
if (!foo) { | |
var foo = 5 | |
} | |
console.log(foo) | |
} | |
bar() | |
function bar() { | |
foo = 666 | |
console.log(foo) | |
function foo() {} | |
} | |
bar() | |
var foo = 0 | |
function bar() { | |
if (!foo) { | |
let foo = 10 | |
} | |
console.log(foo) | |
} | |
bar() | |
// 2 | |
var myObject = { | |
foo: "bar", | |
func: function() { | |
var self = this | |
console.log("outer func: this.foo = " + this.foo) | |
console.log("outer func: self.foo = " + self.foo) | |
(function() { | |
console.log("inner func: this.foo = " + this.foo) | |
console.log("inner func: self.foo = " + self.foo) | |
}()) | |
} | |
} | |
myObject.func() | |
// 3 | |
console.log(0.1 + 0.2) | |
console.log(0.1 + 0.2 == 0.3) | |
// 4 | |
(function() { | |
console.log(1) | |
setTimeout(() => console.log(2), 1000) | |
setTimeout(() => console.log(3), 0) | |
console.log(4) | |
})() | |
// 5 | |
for (var i = 0; i < 5; i++) { | |
var btn = document.createElement('button') | |
btn.appendChild(document.createTextNode('Button ' + i)) | |
btn.addEventListener('click', function(){ console.log(i) }) | |
document.body.appendChild(btn) | |
} | |
// 6 | |
console.log("0 || 1 = "+(0 || 1)) | |
console.log("1 || 2 = "+(1 || 2)) | |
console.log("0 && 1 = "+(0 && 1)) | |
console.log("1 && 2 = "+(1 && 2)) | |
// 7 | |
const a={}, | |
b={key:'b'}, | |
c={key:'c'} | |
a[b]=123 | |
a[c]=456 | |
console.log(a[b]) | |
// 8 | |
const hero = { | |
_name: 'John Doe', | |
getSecretIdentity: function (){ | |
return this._name | |
} | |
} | |
const stoleSecretIdentity = hero.getSecretIdentity | |
console.log(stoleSecretIdentity()) | |
console.log(hero.getSecretIdentity()) | |
// 9 | |
const p3 = new Promise((resolve, reject) => setTimeout(resolve, 100)) | |
const p4 = new Promise((resolve, reject) => setTimeout(reject, 500)) | |
Promise.all([p3, p4]).then( | |
() => console.log('success'), | |
() => console.log('fail') | |
) | |
const p3 = new Promise((resolve, reject) => setTimeout(resolve, 100)) | |
const p4 = new Promise((resolve, reject) => setTimeout(reject, 500)) | |
Promise.race([p3, p4]).then( | |
() => console.log('success'), | |
() => console.log('fail') | |
) | |
// 10 | |
const delay = // ??? | |
Promise.resolve('Adalisk') | |
.then(delay(500)) | |
.then(result => console.log(result)) // "Adalisk" | |
// 11 | |
const foo = { | |
x: 10, | |
y: { | |
name: 'John' | |
} | |
} | |
const bar = { | |
x: 20, | |
...foo | |
} | |
console.log(bar.x) | |
console.log(foo.y === bar.y) | |
// 12 | |
const foo = { | |
a: 1, | |
b: 2, | |
c: 3, | |
d: 4, | |
e: 5 | |
} | |
// ??? | |
// bar = { | |
// c: 3, | |
// d: 4, | |
// e: 5 | |
// } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment