JS tamping Foundation: Javascript Perverted analysis (UP)

Source: Internet
Author: User
Tags pow

["1""2""3"].map (parseint)

1, Knowledge points:

    • Array/map
    • Number/parseint
    • Global_objects/parseint
    • JavaScript parseint

First, map accepts two parameters, a callback function callback, the This value of a callback function. Where the callback function accepts three parameters CurrentValue, index, arrary (current value, indexes, entire array), and in the title, map only passes in the callback function: parseint.

Second,Parseint accepts only two two parameters, string, Radix(cardinality). So this string is equivalent to the parameter Currentvalue,radix is equivalent to the parameter index

// so ask parseint ('1'0);p arseint ('2'  1);p arseint ('3'2); // first, the latter two parameters are not valid. Where 0,1,2 is index// so the answer is [1, Nan, Nan]

The most important thing to understand is to know: (1) The 3 parameter meanings of the callback function of map, (2) The parseint can only accept 2 parameters, and the parameter meaning

[typeofnullnull instanceof Object]

2, two points of knowledge:

    • Operators/typeof
    • Operators/instanceof
    • Operators/instanceof (Medium)

typeof returns a String representing the type.

The instanceof operator is used to detect whether the Constructor.prototype exists on the prototype chain of the Parameter object.

The answer to this question is simple: ["Object", false]. See the following table for the results of typeof:

[[3,2,1].reduce (MATH.POW), [].reduce (MATH.POW)]

3, Knowledge points:

    • Array/reduce

  arr.reduce(callback[, initialValue])

Reduce accepts two parameters, a callback, and an initial value.

A callback function accepts four parameterspreviousValue, currentValue, currentIndex, array

It is important to note thatIf the array is empty and no initialValue was provided, TypeError would be thrown.

So the second expression will report an exception. The first expression is equivalent to anMath.pow(3, 2) => 9; Math.pow(9, 1) =>9

Answeran error

Note: The parameters of reduce and map are different. The equivalent array [3,2,1] becomes the parameter of the callback function completely, then the callback function Math.pow (3,2) = 9, and then aspreviousValue参数传给回调函数。那么第二行的currentValue=1是怎么回事呢?我们再验证下:

That previousValue is, the value of index 0, the index starts at 1, and thencurrentValue就是当前数组的索引的值。然后依次往下,理解这点比较重要。

var ' Smtg ' ; Console.log ("smtg "  Something' Nothing');

4, two points of knowledge:

    • Operators/operator_precedence
    • Operators/conditional_operator

+ In short, the priority ? is greater than

So the original question ‘Value is true‘ ? ‘Somthing‘ : ‘Nonthing‘ is equivalent instead of‘Value is‘ + (true ? ‘Something‘ : ‘Nonthing‘)


varName ='world!';(function () {if(typeofName = = ='undefined') {        varName ='Jack'; Console.log ('Goodbye'+name); } Else{Console.log ('Hello'+name); }})();

5, this relatively simple, a knowledge point:

    • Hoisting

In JavaScript, functions and variables are promoted . Variable elevation is the behavior that JavaScript moves the declaration to the top of the scope scope (global domain or current function scope).

This topic is equivalent to:

varName ='world!';(function () {varname; if(typeofName = = ='undefined') {Name='Jack'; Console.log ('Goodbye'+name); } Else{Console.log ('Hello'+name); }})();//so the answer is ' Goodbye Jack ' .
var END = Math.pow (2); var  - ; var 0 ;  for (var i = START; I <= END; i++) {    count+ + +;} Console.log (count);

6, a knowledge point:

    • Infinity

In JS, Math.pow (2,53) can represent the maximum value, the maximum value plus 1 or the maximum value, so the loop will not stop.

The largest integer that can be represented in JS is not 2 of the 53-time square, but 1.7976931348623157e+308. 2 of the 53-time side is not the largest integer JS can be represented, but should be able to correctly calculate the maximum accuracy without losing the largest integer, you can see the JS authoritative guide. 9007199254740992 +1 or 9007199254740992, this is because of the accuracy problem, if 9007199254740992 +11 or 9007199254740992 +111, the value will change, Just the result of the calculation is not the correct value, because of the problem of loss of precision.

JS tamping Foundation: Javascript Perverted analysis (UP)

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.