kujian / article

Apache License 2.0
8 stars 2 forks source link

据说做对一半才能称之为前端,否则就只能搬砖 #4

Open kujian opened 8 years ago

kujian commented 8 years ago

1.

(function(){
  return typeof arguments;
})();

"object" "array" "arguments" "undefined"

2.

var f = function g(){ 
  return 23; 
};
typeof g();

"number" "undefined" "function" Error

3.

(function(x){
  delete x;
  return x;
})(1);

1 null undefined Error

4.

var y = 1, x = y = typeof x;
x;

1 "number" undefined "undefined"

5.

(function f(f){
  return typeof f();
})(function(){ return 1; });

"number" "undefined" "function" Error

6.

var foo = {
  bar: function() {
    return this.baz; 
  },
  baz: 1
};
(function(){
  return typeof arguments[0]();
})(foo.bar);

"undefined" "object" "number" "function"

7.

var foo = {
  bar: function(){
    return this.baz; 
  },
  baz: 1
}
typeof (f = foo.bar)();

"undefined" "object" "number" "function"

8.

var f = (
  function f(){ 
    return "1"; 
  }, 
  function g(){ 
    return 2; 
  }
)();

typeof f; "string" "number" "function" "undefined"

9.

var x = 1;
if (function f(){}) {
  x += typeof f;
}
x;

1 "1function" "1undefined" NaN

10.

var x = [typeof x, typeof y][1];
typeof typeof x;

"number" "string" "undefined" "object"

11.

(function(foo){
  return typeof foo.bar;
})({ foo: { bar: 1 } });

"undefined" "object" "number" Error

12.

(function f(){
  function f(){ return 1; }
  return f();
  function f(){ return 2; }
})();

1 2 Error (e.g. "Too much recursion") undefined

13.

function f(){ return f; }
new f() instanceof f;

true false

14.

with (function(x, undefined){}) length;

1 2 undefined Error

思考题:

如何改变 undefined的typeof类型 ? (即 typeof undefined 或者 typeof(undefined) 都不为"undefined" ) 下面就是答案了,我只能帮你到这儿了。 http://sentsin.com/jsquiz.html

1
4
1
4
1
1
1
2
3
2
1
2
1
2