请给出这段代码的运行结果
function Foo() {
var i = 0;
return function () {
console.log(i++);
}
}
var f1 = Foo();
var f2 = Foo();
f1(); // 0
f1(); // 1
f2(); // 0
var bb = 1;
function aa(bb) {
bb = 2;
console.log(bb);
};
aa(bb); // 2
console.log(bb); // 1
function t(a) {
var a = 'hello';
console.log(a);
function a() {
console.log(null);
}
console.log(a);
}
t(null);
// hello
// hello
function fn(b) {
console.log(b);
function b() {
console.log(b);
}
b();
}
fn(10);
// [function: b]
// [function: b]
function a(b) {
console.log(b);
b = function () {
console.log(b);
};
b();
}
a();
// undefined
// [Function: b]
function MyObj() {
this.p.pid++;
}
MyObj.prototype.p = { pid: 0 };
MyObj.prototype.getNum = function (num) {
return this.p.pid + num;
};
var _obj1 = new MyObj();
var _obj2 = new MyObj();
console.log(_obj1.getNum(1) + _obj2.getNum(2)); // 7
var func = (function (a) {
this.a = a;
return function (a) {
a += this.a;
return a;
}
})(function (a, b) {
return a;
}(1, 2));
console.log(func(4)); // 5
if(!('a' in window)){
var a = 1;
}
console.log(a); // 1
(function () {
var a = b = 3;
})();
// console.log(a); // ReferenceError: a is not defined
console.log(b); // 3
var myObject = {
foo: 'bar',
func: function () {
var self = this;
console.log(this.foo);
console.log(self.foo);
(function () {
console.log(this.foo);
console.log(self.foo);
}());
}
};
myObject.func();
// bar
// bar
// undefined
// bar
var a = 1;
if (!b in window) {
var b = 2;
a += 1;
}
console.log(a); // 1
console.log(b); // undefined
var m = 1;
function log() {
var m = 2;
return function () {
m += 1;
}
}
var _log = log();
_log();
console.log(m); // 1
for (var i = 0; i < 5; i++){
(function () {
setTimeout(function () {
console.log(i);
}, 1000);
})(i);
}
// 5
// 5
// 5
// 5
// 5
function fun() {}
console.log(typeof fun); // function
console.log(fun instanceof Function); // true
console.log(fun instanceof Object); // true
var a = 1;
var obj = {
a: 2,
getA: function () {
return this.a;
}
};
console.log(obj.getA()); // 2
console.log(obj.getA.call()); // undefined
console.log(obj.getA.call({ a: 10 })); // 10
var arr = [1, 2, 3];
function test(arr) {
arr = [];
}
test(arr);
console.log(arr); // [1, 2, 3]
function Foo() {
getName = function () {
console.log(1);
}
}
Foo.getName = function () {
console.log(2);
};
Foo.prototype.getName = function () {
console.log(3);
};
var getName = function () {
console.log(4);
};
function getName() {
console.log(5);
}
Foo.getName(); // 2
getName(); // 4
Foo().getName(); // TypeError: Cannot read property 'getName' of undefined
getName(); // 4
new Foo.getName(); // 2
new Foo().getName(); // 3
new new Foo().getName(); // 3