You should know the 25-way JavaScript face test __java

Source: Internet
Author: User
Tags object object
you should know the 25-way JavaScript face test

Topics from Essential JavaScript interview Questions. There is nothing to do, just cut it. a

What is a potential pitfall with using typeof bar = = "Object" to determine if bar was an object? How can I pitfall be avoided?

Cliché question, whether TypeOf can accurately judge an object variable, the answer is no, null result is also the result of Object,array is object, sometimes we need is "pure" object.

How to avoid this problem.

var obj = {};

1
console.log ((obj!== null) && (typeof obj = = "Object") && (Tostring.call (obj)!== "[Object Array ]"));

2
console.log (Object.prototype.toString.call (obj) = = "[Object Object]");
two

What'll the code below output to the console and why?

(function () {
  var a = b = 3;
}) ();

Console.log ("a defined?" + (typeof a!== ' undefined '));
Console.log ("B defined" + (typeof b!== ' undefined '));

This problem is not difficult, the assignment procedure in Iife is actually (assignment procedure from right to left):

(function () {
  b = 3;
  var a = b;
}) ();

The next is not difficult, a is a local variable, B is a global variable. three

What'll the code below output to the console and why?

var myObject = {
    foo: ' Bar ',
    func:function () {
        var self = this;
        Console.log ("outer func:  this.foo =" + This.foo);
        Console.log ("outer func:  self.foo =" + Self.foo);
        (function () {
            console.log ("inner func:  this.foo =" + This.foo);
            Console.log ("Inner func:  self.foo =" + Self.foo);
        } ());
    }
};
Myobject.func ();

The previous two output is not a problem, is bar, the problem is the following two. After using the Iife, this in the anonymous function has already pointed to window, so the third output this.foo is actually window.foo, and the global object does not have foo this key, so the output undefined, and the fourth output, because self references the MyObject, so it's bar. Four

What is the significance of, and reason to, wrapping the entire content of a JavaScript source file in a function block?

Why to use Iife.

Simply to be able to modularize, create private variables, and so on, many class libraries (such as jQuery) use this notation.

I can refer to an article in my previous translation. JavaScript immediate execution function expression (iife) five

What is the significance, and What are the benefits, of including ' use strict ' at the beginning of a JavaScript source fil E?

What is the benefit of Javascript development in strict mode?

This will not unfold, you can refer to the Nanyi Teacher's Javascript Strict mode of detailed explanation or Google Baidu. Six

Consider the two functions below. Would they both return the same thing? Why or Why not?

function foo1 ()
{return
  {
      bar: ' Hello '
  };
}

function Foo2 ()
{
  return
  {
      bar: ' Hello '
  };
}

Perform the two functions above, will return the same thing.

No, the second function returns undefined. This is due to Javascript's seal insertion mechanism. If a line of code, return keyword after nothing, will automatically insert a number, obviously foo2 function, when a return after the insertion of a number, although the following statement does not comply with the rules, but because there is no execution to , so it won't be an error. There is no function to return anything, and the default returns undefined.

So many Javascript specifications recommend {write {on a line, not another row. Seven

What is NaN? What is it type? How can you reliably test if a value was equal to NaN?

NaN is what ghost. The result of TypeOf is. If the value of a variable is NaN, how to determine.

NaN is the abbreviation for ' not a number ', which means ' not a digit ' and is usually generated during the operation:

Console.log (' abc '/4);
Console.log (4 * ' a ');

Although it is "not a number", the typeof result of NaN is numbers:

Console.log (typeof (4 * ' a ')); Number

Nan and any variable are not equal, including Nan himself:

Console.log (nan = = Nan); False

To judge whether a variable is NaN can use the isNaN () function, but this is not a perfect function, sometimes with value!== value seems more accurate, fortunately, ES6 already have Number.isnan () method, will be more accurate than isNaN (). Eight

What'll the code below output? Explain your answer.

Console.log (0.1 + 0.2);
Console.log (0.1 + 0.2 = 0.3);

What the output of the above code is.

This question just I have studied before, interested can refer to under "0.1 + 0.2 = 0.30000000000000004" How to understand. , I understand the interest can also see this article Yuber a lesson after the problem (about IEEE 754 double-precision floating-point precision loss) nine

Discuss possible ways to write a function isinteger (x) This determines if X is an integer.

Write a method Isinterger (x) that can be used to determine whether a variable is an integer.

The Number.isinteger () method is used in the ES6. But at present, there is no ES5 method, you can remove a number of decimal points and the number of comparisons, to determine whether the equality, then the problem evolved into how to take a number of the whole.

var a = -1.2223;
 Console.log (a ^ 0);  -1
 Console.log (A | 0);  -1
 Console.log (a << 0);//-1
 console.log (a >> 0);//-1

 console.log (Math.Round (a));// -1
  
   console.log (Math.floor (a)); -2
 Console.log (Math.ceil (a));  -1
  
10

In what order would the numbers 1-4 be logged to the console when the code below is executed? Why?

(function () {
    console.log (1); 
    settimeout (function () {Console.log (2)}, 1000); 
    settimeout (function () {Console.log (3)}, 0); 
    Console.log (4);
}) ();

The output of the above code is.

This problem is not difficult, as long as you know Javascript is a single-threaded language, some asynchronous events are executed after the main JS execution, so the main body of 1, 4 first output, and then 3, 2, no problem, because 3 of the timing set than 2 earlier.

You can refer to my previous article from settimeout to talk about JavaScript operating mechanism 11

Write a simple function (less than characters) This returns a Boolean indicating whether or not a string is a palindrom E.

Judge whether a string is a palindrome.

function Ispalindrome (str) {
    str = str.replace (/\w/g, '). toLowerCase ();
    return (str = = Str.split ("). Reverse (). Join ("));

Here think of a progressive problem, to find the longest palindrome string, you can refer to the longest palindrome string-Leetcode 5. Longest palindromic Substring 12

Write a sum method which would work properly when invoked using either syntax.

Console.log (sum (2,3));   Outputs 5
console.log (SUM (2) (3));  Outputs 5

Write a sum method that enables the above code to get the desired result. This question can refer to my previous article Uncle Tom's 6-way JavaScript programming problem in the last question, theoretically this problem is simpler, because it does not require expansion (such as SUM (2) (3) (4)), can even:

function sum (x) {
  if (Arguments.length = 2) {return
    arguments[0] + arguments[1];
  } else {return
    functi On (y) {return x + y;};}}

or so:

function sum (x, y) {
  if (y!== undefined) {return
    x + y;
  } else {return
    function (y) {return x + y;};
  }
}
13

Consider the following code snippet:

for (var i = 0; i < 5; i++) {
  var btn = document.createelement (' button ');
  Btn.appendchild (document.createTextNode (' Button ' + i));
  Btn.addeventlistener (' click ', Function () {console.log (i);});
  Document.body.appendChild (BTN);
}

(a) What gets logged to the console while the user clicks on "button 4" and why?

(b) Provide one or more alternate implementations that would work as expected.

Click on ' Button 4 ' to output what. How to make the output the same as expected.

The answer is output 5, in fact, click on any button, the output is 5. Since the end of the loop, the I value becomes 5. How to change, make output is 0, 1, 2, 3, 4 respectively. Using closures in memory to save variables, you can refer to my previous article on these 10 JavaScript pen questions you will be the 8th question. 14

What'll the code below output to the console and why?

 var arr1 = "John". Split ("); var arr2 = Arr1.reverse (); v 
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.