JS基礎二(経典問題型及び答え)
JS経典基礎問題型
<script>
var i = 5;
function fn(i) {
return function (n) {
console.log(n + (++i));
}
}
var f = fn(1);
f(2); // 4
fn(3)(4); // 8
fn(5)(6); // 12
f(7); // 10
console.log(i); // 5
</script>
<script>
var i = 20;
function fn() {
i -= 2;
var i = 10;
return function (n) {
console.log((++i) - n);
}
}
var f = fn();
f(1); // 10
f(2); // 10
fn()(3); // 8
</script>
<script>
let x = 5;
function fn(x) {
return function (y) {
console.log(y + (++x));
}
}
let f = fn(6);
f(7);// 14
fn(8)(9);// 18
f(10);//18
console.log(x);// 5
</script>
<script>
let x = 5;
function fn() {
return function (y) {
console.log(y + (++x));
}
}
let f = fn(6);
f(7); // 13
fn(8)(9); // 16
f(10); // 18
console.log(x); // 8
</script>
<script>
let a = 0;
b = 0;
function A(a) {
A = function (b) {
alert(a + b++)
}
alert(a++)
}
A(1); // 1
A(2); // 4
</script>
<script>
var t = (function (i) {
return function () {
alert(i *= 2);
}
})(2);
t(5); // 4
</script>
<script>
var n = 0;
function a() {
var n = 10;
function b() {
n++;
console.log(n);
}
b(); // 11
return b;
}
var c = a();
c(); // 12
console.log(n); // 0
</script>
<script>
var a = 1;
function fn(a) {
// EC(fn)
// 1) 2)
// AO a = 1 a = a = 2
console.log(a);
var a = 2;
function a() { console.log("ok") }
console.log(a); // 2
}
fn(a);
console.log(a); // 1
</script>
<script>
var a = 1;
function b() {
if (!a) {
var a = 2;
}
console.log(a) // 2
}
b();
</script>
<script>
var a = 10,
b = 11,
c = 12;
function f(a) {
a = 1;
var b = 2;
c = 3;
}
f(10);
console.log(a); //10
console.log(b); //11
console.log(c); //3
</script>
<script>
if (!('a' in window)) {
var a = 110;
}
console.log(a); //undefined
</script>
<script>
let a = 0,
b = 0;
function A(a) {
A = function (b) {
alert(a + b++)
}
alert(a++)
}
A(1)
A(2)
// 1 4
</script>
<script>
let a = 0,
b = 0;
function A(a) {
// A
var A = function (b) {
alert(a + b++)
}
alert(a++)
}
A(1)
A(2)
// 1 2
</script>
<script>
let a = 0,
b = 0;
function A(a) {
B = function (b) {
alert(a + b++)
}
alert(a++)
}
A(1)
B(2)
// 1 4
</script>
<script>
let a = 0,
b = 0;
function A(a) {
A = function (b) {
alert(a + b++)
}
alert(a++)
return A;
}
A(1)(2)
// 1 4
</script>
<script>
g();
console.log(a); //undefined
var a = 110;
function g() {
k();
console.log(b);//undefined
console.log("g...")//g...
var b = 2;
console.log(b)//2
function k() {
console.log("k...");//k...
}
}
</script>