5 Classic front-end interview questions

Source: Internet
Author: User
Javascript Developers in IT The demand for the sector has been very large. If you are very proficient in the language of God, you will have a lot of opportunities to change jobs and raise salaries. But before a company hires you, you have to pass the interview and prove your skills. In this article, I'll show you 5 Issues related to the front end to test the candidate's Javascript Skills and their ability to solve problems. There will be very interesting!

Issue 1 : Scope scope

Consider the following code:

(function () {

var a = b = 5;

})();

Console.log (b);

What will be printed on the console?

Reply

The above code will print 5 .

The trick to this question is that there are two variable declarations, but A is declared with the keyword var . Represents a local variable of a function. In contrast, b becomes a global variable.

Another trick of this question is that it does not use strict mode (' using strict ';) . If strict mode is enabled, the code throws a referenceerror error: B has no definition ( c7> b is not defined ). Keep in mind that strict mode requires explicit designation in order to implement a global variable declaration. For example, you should write:

(function () {

' Use strict ';

var a = window.b = 5;

})();

Console.log (b);

Issue 2 : Create a "native" ( native ) method

Defines a repeatify function for a string object . When an integer n is passed in, it returns the result of repeating the n-th string. For example:

Console.log (' Hello '. repeatify (3));

The Hellohellohello should be printed .

Reply

One possible implementation is as follows:

String.prototype.repeatify = String.prototype.repeatify | | function (times) {

var str = ';

for (var i = 0; I < times; i++) {

str + = this;

}

return str;

};

Now the question is tested by developers about JavaScript inheritance and prototype knowledge points. This also verifies whether the developer knows if the built-in object is extended (although this should not be done).

Another important point here is that you need to know how to not overwrite features that might already be defined. Do not exist until you test the function definition:

String.prototype.repeatify = String.prototype.repeatify | | function (times) {/* code here */};

This technique is especially useful when you are asked to do JavaScript functions that are compatible.

Issue 3 : Claim elevation ( hoisting )

Execute this code and output what results.

function Test () {

Console.log (a);

Console.log (foo ());

var a = 1;

function foo () {

return 2;

}

}

Test ();

Reply

The result of this code is undefined and 2 .

The reason is that the declarations of variables and functions are advanced (moved to the top of the function), but the variables do not have any values assigned. Therefore, when the variable is printed, it exists in the function (it is declared), but it is still undefined . In other words, the above code is equivalent to the following:

function Test () {

var A;

function foo () {

return 2;

}

Console.log (a);

Console.log (foo ());

A = 1;

}

Test ();

Issue 4 : This is in JavaScript as of what works

What result does the following code output? Give your answer.

var fullname = ' John Doe ';

var obj = {

FullName: ' Colin ihrig ',

Prop: {

FullName: ' Aurelio De Rosa ',

Getfullname:function () {

return this.fullname;

}

}

};

Console.log (Obj.prop.getFullname ());

var test = Obj.prop.getFullname;

Console.log (Test ());

Reply

The answer is Aurelio De Rosa and John Doe . The reason is that in a function, the behavior of this depends on the JavaScript The way a function is called and defined, not just how it is defined.

In the first Console.log () Call, Getfullname () is called as Obj.prop The function of the object. So, the context refers to the latter, and the function returns the object's fullname Getfullname () was assigned to test window test window fullname

Issue 5 : Call () and apply ()

Now let you solve the previous problem and make the final console.log () print Aurelio De Rosa .

Reply

The problem can change the function context by forcing the use of call () or apply () . I will use call () below , but in this case, apply () will output the same The result:

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

Conclusion

In this article, we've discussed the five classic questions that are used to test a JavaScript developer. The concept of interviewing and the topics covered are usually very similar. If you don't know the answer to some of the questions, don't worry: learning and experience can accumulate slowly.

If you have some other interesting questions, don't hesitate to share them with us. It will help many developers.

free pick up LAMP Brother Lian original PHP video tutorial CD detailed php "Essentials Edition, details Enquiry Service:

Http://www.lampbrother.net

Phpcms two times development http://yun.itxdl.cn/online/phpcms/index.php?u=5

Development http://yun.itxdl.cn/online/weixin/index.php?u=5

Mobile Internet server-side development http://yun.itxdl.cn/online/server/index.php?u=5

Javascript Course http://yun.itxdl.cn/online/js/index.php?u=5

CTO Training Camp http://yun.itxdl.cn/online/cto/index.php?u=5

The above describes 5 classic front-end interview questions, including the aspects of the content, I hope that the PHP tutorial interested in a friend to help.

  • 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.