I have seen a set of JavaScript topics before, and I would like to share them with you. With their own understanding and answers. We welcome you to point out and discuss the controversial points.

Subject to a

(function(){ return typeof arguments; }) ();Copy the code

The answer: “object”

Arguments are objects, pseudo arrays there are two things to note here:

Parameter is not an Array, it is an Array of objects, you can use the brackets and integer index of the element, but the method usually don’t live in a such as put parameters Array Array. The prototype. Slice. The call (the arguments); Returns “object” even if arguments are arrays, since arrays are also objects. Additional: Typeof judgment on type

https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Operators/typeof

Topic 2

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

Answer: Mistakes happen

Function g(){return 23; } is a function expression that actually has only one name, not a function declaration

The function is actually bound to the variable f, not g.

Specifying identifiers in function expressions has its uses though: stack traces are clear rather than riddled with nameless functions, and you can have an anonymous function recursively called itself without using argument.callee

With very detailed post function expression, http://kangax.github.io/nfe/

The title three

(function(x){ delete x; return x; }) (1);Copy the code

Answer: 1.

The title four

var y = 1, x = y = typeof x;
x;Copy the code

The answer: “undefined”

By rewriting the code as follows:

var a, b; The expansion is var a; var b; .

A = B = C; B is equal to C is equal to B; Knowing this, we rewrite and get:

var y = 1;
y = typeof x;
var x = y;
x;Copy the code

When y = typeof x is executed, x has not yet been defined, so y becomes the string “undefined” and is then assigned to x.

Topic five

(function f(f){
  return typeof f();
})(function(){ return 1; });Copy the code

The answer: “number”

To make it easier to understand, we continue to break it down:

The first part

var baz = function(){ return 1; };Copy the code

The second part

(function f(f){
  return typeof f();
})(baz);Copy the code

Here, function F takes one argument to be another function, which internally executes the argument function and returns the type

Whether it returns from calling the function, even if the parameter name conflicts with the function name, the function accepts itself as an argument, and then calls, depending on who has the higher priority

Topic 6

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

(function(){
  return typeof arguments[0]();
})(foo.bar);Copy the code

The answer: “undefined”

Why undefined? We need to know how the this operator works.

JS language essence summary is very refined:

  1. Pure function calls

  2. A call as an object method

  3. Called as a constructor

  4. The apply call

So let’s see what kind of environment are they in?

Arguments0 (arguments0); arguments[0] is the foo. Bar method. Note that this in foo.bar is not bound to foo. Although foo.bar is passed to functions, the actual execution of function bar is in the context of arguments, not foo.

Arguemnts [0] arguemnts[0] arguemnts[0] arguemnts[0] arguemnts[0] arguemnts[0] arguemnts[0] arguemnts[0] arguemnts[0] arguemnts[0] arguemnts[0] arguemnts[0] arguemnts[0] arguemnts[0] arguemnts[0] arguemnts The window has no baz property and returns undefined. Typeof calls are converted to undefined.

Title seven

var foo = {

bar: function(){ return this.baz; },

baz: 1

}

typeof (f = foo.bar)();Copy the code

The answer to “undefined”

Let me rewrite this:

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

f = foo.bar;
typeof f();Copy the code

Foo. Bar is stored to f and called, so this references global objects in foo.bar, so there is no baz property. In other words, foo.bar is executed in the context of foo, but when foo. Bar is assigned to f, the context of f is window and there is no baz, so it is “undefined”.

The title eight

var f = (function f(){ return "1"; }, function g(){ return 2; }) (); typeof f;Copy the code

The answer “number”

The use of the comma operator can be confusing, but this shows how it behaves:

var x = (1, 2, 3);
x;Copy the code

The value of x is 3, which indicates that when you have a series of expressions combined and separated by commas, they evaluate from left to right, but only the result of the last expression is saved. For the same reason, the problem can be rewritten to reduce confusion:

var f = (function g(){ return 2; }) (); typeof f;Copy the code

Title nine

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

The answer “1 undefined”

Function f(){} function f(){} The actual rules for function declarations are as follows:

Function declarations can only occur in the body of a program or function. Syntactically, they cannot occur in a Block ({… }), for example, cannot appear in if, while, or for statements. Blocks can only contain statements, not source elements such as function declarations. On the other hand, a closer look at the rules shows that the only way an expression can occur in a Block is if it is part of an expression statement. However, the specification explicitly states that expression statements cannot begin with the keyword function. This means, in effect, that function expressions also cannot appear in statements or blocks (because blocks are made up of statements).

Suppose we change the code:

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

x;

var x = 1;
x += typeof f;
x;Copy the code

F is not defined here, so typeof F is the string “undefined”, the sum of characters and numbers is a string, so the final x is “1undefined”.

The title ten

(function f(){
  function f(){ return 1; }
  return f();
  function f(){ return 2; }
})();Copy the code

Answer 2

If you look all the way down, this problem should be relatively easy. Simply put, a function declaration is parsed and evaluated before any expression is parsed and evaluated before a return. Even if your declaration is on the last line of code, it will be parsed/evaluated before the first expression in the same scope.

In the following example, the function fn is declared after alert, but by the time alert is executed, fn is already defined

alert(fn()); function fn() { return 'Hello world! '; }Copy the code

So the function is promoted twice, and the second time overwrites the first one, so the f after return is the function declared by the next statement in the return statement. Function f (){})(); F in f does not have a function promotion effect, it is an expression.

The title.

function f(){ return f; }
new f() instanceof f;Copy the code

The answer to false

How to understand?

New f(), first this operation creates a new object and calls the constructor function as its current context object. To put it simply,

new f();

I vaguely remember it said in advanced programming:

  1. Create an empty object.

  2. Copy the properties and methods from the class’s Prototype into the instance.

  3. Call the class constructor with the empty object created in the first step as an argument to the class

By default, this is returned if the empty object is not overridden

var a = new Object;Copy the code

A instanceof Object is true when we look at f() and return f; So the new object is itself, and the constructor itself returns an instance of that object during new. But the return value of the function overrides the instance, and the new becomes null and void. Function f(){return this} or function f(){}.

var a = new f();

a instanceof f  // falseCopy the code

It’s worth noting that Instanceof tests prototypes.

The title.

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

typeof typeof x;Copy the code

This is an easy problem, just pay attention to the return type. X = [and] [1]; That is x = typeof y = ‘undefind’.

Typeof returns a string, so typeof typeof must be ‘string’.

Topic 13

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

The answer to “undefined”

Another disgusting question, pure word play, take a close look.

Let’s break it down

var baz = { foo: { bar: 1 } };

(function(foo){
  return typeof foo.bar;
})(baz);Copy the code

Remove function association

var baz = { foo: { bar: 1 } };

var foo = baz;

typeof foo.bar;Copy the code

Finally, we remove the intermediate variable foo by substitution

var baz = { foo: { bar: 1 } };
typeof baz.bar;Copy the code

So now it’s clear that baz is not defined in the property; It is defined as baz.foo, so the result is: “undefined”.