第一题
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
console.log(a);
输出: 1
第二题
function foo(){
function bar() {
return 3;
}
return bar();
function bar() {
return 8;
}
}
alert(foo());
输出: 8
第三题
function parent() {
var hoisted = "I'm a variable";
function hoisted() {
return "I'm a function";
}
return hoisted();
}
console.log(parent());
输出: “TypeError: hoisted is not a function”
第四题
alert(foo());
function foo() {
var bar = function() {
return 3;
};
return bar();
var bar = function() {
return 8;
};
}
输出: 3
第五题
var myVar = 'foo';
(function() {
console.log('Original value was: ' + myVar);
var myVar = 'bar';
console.log('New value is: ' + myVar);
})();
输出: “Original value was: undefined”, “New value is: bar”
第六题
console . log(to) //undefined
var to=1;
function fn(n1,n2){
console . log(to) //1
to=n1+n2;
console . log(to) //30
}
fn(10,20)
console . log(to) //30
第七题
function test(a,b){
console . log(b) //function
console . log(a) //1
c=0;
a=3;
b=2;
console . log(b); //2
function b(){ }
function d(){ }
console . log(b) //2
}
test(1)
第八题
function foo() {
console.log( this.a );
}
var obj1 = {
a: 2,
foo: foo
};
var obj2 = {
a: 3,
foo: foo
};
obj1.foo(); // 2
obj2.foo(); // 3
obj1.foo.call( obj2 ); // 3
obj2.foo.call( obj1 ); // 2
第九题
var A = function( name ){
this.name = name;
};
var B = function(){
A.apply(this,arguments);
};
B.prototype.getName = function(){
return this.name;
};
var b=new B('sven');
console.log( b.getName() ); // 输出: 'sven'
第十题
function foo() {
setTimeout(function() {
console.log('id:', this.id);
});
}
var id = 21;
foo.call({ id: 42 }); //21
//注意定时器,此时this指向window
来源:CSDN
作者:拳是套路
链接:https://blog.csdn.net/weixin_43750811/article/details/104300030