5 Classic JavaScript face questions

Source: Internet
Author: User


The requirements for JavaScript developers in the IT community are still high, but if JavaScript developers have the skills and experience to reach a certain level, they can easily jump to a good company, and of course the salary is not a problem. But before the interview, the interview preparation should be sufficient enough, after all, not every good developer can express themselves in a short period of time. In this article, I'll list 5 common front-end development questions. See here you prove that you must be a programmer or HR, not to read the article, to understand what the shortcomings of their own, what the problem I hope you can communicate in the message.

Issue 1: Scope

Take a look at the following code:

    (function() {    var a = b =5;    }) ();    Console.log (b);

What does the result output?

Answer:

5

The point of this question is that two different scopes, ' a ' is declared by Var as a local variable, but ' B ' is not actually defined, so it is a global variable.

This problem is also related to another important issue, that is strict mode, if you choose Strict mode, the above code will be reported uncaught Referenceerror, because B is not defined, it can help you check out some of the code problems:

    (function() {    ' use strict ';     var a = window.b =5;    }) ();    Console.log (b);
Issue 2: Create a "native" method

Write a method that repeats the string object, and enter an integer that represents the number of repeated printed words, such as:

    1. Console. Log(' hello '). Repeatify(3));
This will print out the Hellohellohello. Answer:

One way to do this is to:

    String.prototype.repeatify =string.prototype.repeatify | | function (times) {    var str = '    ;  for (var i =0; i < times; i++) {    += this;    }     return str;    };

This question examines how well developers understand JavaScript inheritance and prototype attributes.

Question 3:hoisting

What is the output of the following code?

    function Test () {    console.log (a);    Console.log (foo ());     var a =1;     function foo () {    return2;    }    }    Test ();
Answer

Undefined and 2.

The above code is equivalent to the following paragraph:

    function Test () {    var  A;     function foo () {    return2;    }    Console.log (a);    Console.log (foo ());     =1;    }    Test ();
Question 4: How to perform in JavaScript

Give the results in the following code to explain your answer:

    var fullname = ' John Doe ';     var obj ={    fullname:' Colin ihrig ',    prop:{    fullname:' Aurelio De Rosa ',    Getfullname: function () {    returnthis.fullname;}}    ;    Console.log (Obj.prop.getFullname ());     var test = obj.prop.getFullname;    Console.log (Test ());
Answer:

Aurelio De Rosa and John Doe.

Question 5:call () and apply ()

After solving the previous problem let the last Console.log () output Aurelio De Rosa.

Answer:

The question is whether call () or apply (). If you don't know the difference between them, I'll build you first read what ' s the difference between Function.call and function.apply? The following line of code I use Call (), but in this case apply () will also produce the same result:

Console.log (Test.call (Obj.prop));

Original from: 5 classic JavaScript face questions

5 Classic JavaScript face questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.