7 quweicunzhen javascript face test _javascript tips

Source: Internet
Author: User
Tags closure javascript array hasownproperty

Here are 7 JavaScript interview questions you should ask before the interview. Otherwise, there is a good chance of wasting your time.
1. What are the two ways to create a JavaScript object?
This is a very simple question, if you've ever used JavaScript. You have to know at least one way. But, still, in my experience, many people who claim to be JavaScript programmers say they don't know how to answer this question.

    • Use the "new" keyword to invoke a function.
    • Open/close curly braces.

var o = {};
You can also continue to ask, "Where do you create objects using the New keyword?" "But because I'm just trying to get rid of some people, I'm going to wait until the real interview," he said.
2. How do I create an array?
This is the same level of problem as "how to create an object." However, some people answered the first question, but could not answer the question.
With the following code, you can simply create an array:
var myarray = new Array ();
Creating an array is a very complex process. But I'd like to hear the answers to the square brackets from the candidate's mouth.
var myarray = [];
Of course, we can continue to ask other questions, such as how to efficiently delete repeating elements in a JavaScript array , and so on, but since we just need to know if the candidate is worth further observation, I'll end up with the questions about the array.

Again, for how to efficiently delete the repeating elements in a JavaScript array , say: JS How to implement the array to rearrange the 5 ways.

The specific method introduction:

1). Traversal Array method

The simplest way to weight, the idea: Create a new array, iterate through the incoming array, and add the new array in the new array; Note: Whether the value is in the array method "IndexOf" is the ECMAScript5 method, IE8 the following is not supported, you need to write more compatible with the lower version of the browser code, The source code is as follows:

The simplest array goes to the heavy method
function unique1 (array) {
 var n = [];//A new temporary array
 //traversal of the current array for
 (var i = 0; i < array.length i + +) {
  //If the current array has already been saved in the temporary array, skip,
  //or push the current item to the temporary array if
  (N.indexof (array[i]) = = 1) n.push (Array[i]);
 } return
 n;
}
To determine whether the browser supports INDEXOF, indexOf for the EcmaScript5 new method IE8 the following (including IE8, IE8 only support part ECMA5) does not support
if (! ARRAY.PROTOTYPE.INDEXOF) {
 //new IndexOf method
 Array.prototype.indexOf = function (item) {
  var result =-1, A_ item = NULL;
  if (This.length = = 0) {return result
   ;
  }
  for (var i = 0, len = this.length i < len; i++) {
   a_item = this[i];
   if (A_item = = Item) {Result
    = i;
    break;
   } 
  }
  return result;
 }

2). Object key value pair method

This method executes faster than any other method, that is, it occupies a larger amount of memory; Realization idea: A new JS object and a new array, traversing the incoming array, determine whether the value is a JS object key, not words to add the key to the object and put in the new array. Note: When determining whether a JS object key, will automatically execute the incoming key "toString ()", different keys may be mistaken for the same; for example: A[1], a["1"]. To solve the above problems, we have to call "IndexOf".

Fastest, takes up space (space for time)
function unique2 (array) {
 var n = {}, R = [], Len = Array.Length, Val, type;
  for (var i = 0; i < Array.Length i++) {
    val = array[i];
    Type = typeof Val;
    if (!n[val]) {
      N[val] = [Type];
      R.push (val);
    } else if (N[val].indexof (type) < 0) {
      n[val].push (type);
      R.push (val);
    }
  return r;
}

3). Array Subscript Judging method

Still have to call "IndexOf" performance is similar to Method 1, the realization of the idea: if the current array of the first occurrence of the current array position is not I, then that the item I is repeated, ignored. Otherwise, the result array is saved.

function Unique3 (array) {
 var n = [array[0]];//result array//
 (var i = 1; i < Array.Length; i++) {
  If the first occurrence of the current array in the current array is not I,
  //It means that item I is duplicated and ignored. Otherwise deposit the result array
  if (Array.indexof (array[i]) = = i) N.push (Array[i]);
 }
 return n;
}

4). After sorting the adjacent removal method

Although the "sort" method of the native array results in a less reliable order, this shortcoming has no effect on the sequential weight. Implementation idea: Sort the incoming array, sort the same values next to each other, and then traverse the newer array to add only values that are not duplicates of the previous value.

Adjacent to the same value, and then traversal to remove the duplicate value
function unique4 (array) {
 array.sort (); 
 var re=[array[0]];
 for (var i = 1; i < Array.Length i++) {
  if (Array[i]!== re[re.length-1])
  {
   Re.push (array[i));
 }
 return re;
}

5). Optimized traversal Array method

From a foreign blog, the implementation code of this method is rather cool; realize: Get the right one without repeating and put in the new array. (The next round of judgment that terminates the current loop and goes to the top loop when duplicate values are detected)

Train of thought: get the right one with no duplicates put the new array
function Unique5 (array) {
 var r = [];
 for (var i = 0, L = array.length i < l i++) {for
  (var j = i + 1; j < L; j + +)
   if (array[i] = = Array[j]) j = ++i;
  R.push (Array[i]);
 return r;
}

3. What is a variable elevation (Variable hoisting)?
this question is a little bit difficult, and I don't ask the other person to answer it. However, this issue can quickly determine the skill level of the candidates: do they really understand the programming language as they have stated?
Variable elevation means that, regardless of where the variable is declared within a scope, the JavaScript engine moves the declaration to the top of the range. If you declare a variable in the middle of a function, such as assigning a variable to a row:

function foo ()
{
 //here omits some code
 var a = "abc";
}
The code will actually run like this:
function foo ()
{
 var A;
 Some code
 a = "abc" is omitted here
;

4. What are the risks of global variables and how do you protect your code from interference?
the danger of global variables is that other people can create variables of the same name, and then overwrite the variables you're using. This is a vexing problem in any language.
There are many ways to prevent it. One of the most common methods is to create a global variable that contains all other variables:
var applicationname = {};
Then, whenever you need to create a global variable, attach it to an object.
applicationname.myvariable = "abc";
Another approach is to encapsulate all the code into an automatically executed function, so that all declared variables are declared within the scope of the function.

(function () {
 var a = "abc";
}) ();

In reality, both of these methods are likely to be used.
5. How do I iterate through a member variable in a JavaScript object?

For (var prop in obj) {
 //bonus points for hasOwnProperty
 if (Obj.hasownproperty (prop)) {
  //do something Here
 }
}

6. What is closure (Closure)?
closures allow a function to be defined within the scope of another external function, and even if everything else in the scope disappears, it can still access variables within that external function. If a candidate is able to explain the risk of using a closure in the For/next loop without declaring a variable to hold the current value of the iteration variable, you should add points to the other person.
7. Describe the JavaScript unit tests that you have experienced.
in fact, we just want to see if the candidate actually did the JavaScript unit test. This is an open-ended question and there is no specific correct answer, but at least the other person has to be able to tell a few things about the process.

The above is for everyone to prepare the JavaScript interview questions, have done very well, I hope everyone in the interview before the leak fill, smooth clearance.

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.