Front-end development interview questions collection (js part)

1. Q: What is the operation result of "1"+2+"3"+4 in js?

Answer: 1234

In js, the string and the numerical value are added, and the result is still a string, and the result 1234 here is also a string.

2. Question: What is the result of the operation of 4+3+2+"1"?

Answer: 91 (Operation from left to right, the previous value is added to get 9, and then the string is added to get 91 string.)

3. Q: In the following code, what is the result?

copy code
var foo = 1;
function bar() {
    foo = 10;
    return;
    function foo() {}
}
bar();
alert(foo);
copy code

Answer: It will output 1. (here window.foo is accessed, not foo in bar)

4. Q: In the following code, what is the result?

copy code
function bar() {
    return foo;
    foo = 10;
    function foo() {}
    var foo = 11;
}
alert(typeof bar());
copy code

Answer: The output function.(var foo = 11 Although the definition is placed after, the variable will be promoted, so the final result is function)

5. Q: In the following code, what is the result?

copy code
var x = 3;
var foo =
    x: 2,
    baz: {
        x: 1,
        bar: function() {
            return this.x;
        }
    }
}
var go = foo.baz.bar;
alert(go());
alert(foo.baz.bar());
copy code

Answer: 3, 1.

go = foo.baz.bar; go() at this time this points to widget. The value of window.x is 3; foo.baz.bar() this time points to baz, and the value of baz.x is 1.

6. Q: In the following code, what is the result?

copy code
var x = 4,
    obj = {
        x: 3,
        bar: function() {
            var x = 2;
            setTimeout(function() {
                var x = 1;
                alert(this.x);
            }, 1000);
        }
    };
obj.bar();
copy code

Answer: Output 4.

The setTimeout method is hung under the window object. setTimeout(anonymous function, time), the anonymous function here forms a closure, so that the local variables of the outer function can be accessed. That is, the x in the window. ( reference )

7. Q: In the following code, what is the result?

copy code
x = 1;
function bar() {
    this.x = 2;
    return x;
}
var foo = new bar();
alert(foo.x);
copy code

Answer: Output 2.

Here this points to the object instance of bar.

8. Q: In the following code, what is the result?

function foo(a) {
    alert(arguments.length);
}
foo(1, 2, 3);

Answer: 3.

9. Q: In the following code, what is the result?

var foo = function bar() {};
alert(typeof bar);

答:undefined

10. Q: In the following code, what is the result?

var arr = [];
arr[0]  = 'a';
arr[1]  = 'b';
arr.foo = 'c';
alert(arr.length);

Answer: 2, the value of output arr is ["a", "b"], I don't quite understand the result here.

11. Q: In the following code, what is the result?

function foo(a) {
    arguments[0] = 2;
    alert(a);
}
foo(1);

Answer: 2

12. Q: In the following code, what is the result?

function foo(){}
delete foo.length;
alert(typeof foo.length);

Answer: number; the value of foo.length is still 0. delete cannot delete, refer to

13. Q: In the following code,

copy code
var name="the window";
var object={
  name:"my object",
  getName: function(){
    return this.name;
  } }
copy code

by calling

object.getName();
(object.getName)();
(object.getName = object.getName)()

turn out?

answer:

In the first line of code, this points to object, so there is no doubt; although the second line of code is parenthesized, it seems that it is only referencing a function, but the value of this is maintained. Because object.getName and (object.getName) are defined the same. The third line of code executes an assignment statement first, and then calls the result of the assignment. Because the value of this assignment expression is the function itself, the value of this cannot be maintained, and the result is returned the window.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324974909&siteId=291194637