Kneeling in a certain degree of Autumn recruit the Web front-end written questions

Source: Internet
Author: User

Tonight ran to Chinese laborers to participate in Baidu Web front-end of the written test, finished after the paper, originally felt to be "for the Si Gu, for it".
One of the topics is about the array, back on the computer above a knock, suddenly for their IQ and knowledge ashamed.

If you don't say anything, go straight to the subject. It's probably Jiangzi:

The code is as follows, please write out the output result.

1<script>2 varstr = ' John ';3 varstr2 = ' Angus ';4 5 vararr1 = Str.split (");6 varARR2 =arr1.reverse ();7 vararr = str2.split (' ');8 varARR3 =Arr2.push (arr);9 TenConsole.log (Arr1.length, Arr1.slice (-1)); OneConsole.log (Arr3.length, Arr3.slice (-1)); A</script>

Simply say that the blogger's low IQ is the place, is the 8th line of push as a direct in the back of the arr2 to add the elements of arr. Alas, I have to say, this is how brain-crippled, how naïve.

Okay, nonsense don't say, first say the answer under FF test:

The 10th line prints: 5 Array [array[5]];

Line 11th does not print the result. (It makes me very laborious ... I'll tell you later. )

You, you guessed it?

Bloggers did not guess two points:

1, the 10th line shows that the length of the arr1 is "5"; (But if I didn't make the mistake of understanding the wrong push (), I wouldn't have guessed the result);

2, the 11th line incredibly did not have any output, Bo Master tried n++ times can not believe.

Finally, the efforts of Baidu (alas, still can't escape the palm of the Niang), finally solved the above two problems (as a just getting started, there is not a problem of the place) ...

first question , see http://www.cnblogs.com/0banana0/archive/2011/11/17/2252639.html:

"Conclusion: JS array is a reference type, it only allows to get or change the value of the array by index reference type things are not through (it is assigned to the external variable), that is (it is assigned to the external variable) this value changes the original array will not have any changes. "

So while we are applying the push () method on ARR3, the end result is an array of arr1, ARR2, and ARR3.

The comments on the first floor of the above blog are clear:

There is no pointer in JS, only the area of the value (value) and the address (reference reference)
var a = [1,2,3,4]//a is not only an array, but also an object, in fact A is a reference to [1,2,3,4]
var b=a
var c=a
The above two assignment statements establish a reference to B and C for a, [1,2,3,4], whether changing a or B or C are operations on [1,2,3,4], which is the address (in-heap operation)
var d=a[1]//is the value of a[1] "1" passed to D, the change of D will not affect a[1], that is, the so-called value (operation in the stack)

The second problem is the 11th line of code no output problem, and finally I got the answer on Sina blog (http://blog.sina.com.cn/s/blog_51c99bb20101o48m.html):

unshift () method adds one or more elements to the beginning of the array and returns the new length. The push () method adds one or more elements to the end of the array and returns the new length. the Pop () method is used to delete and return the last element of the array. The Shift () method removes the first element from the array and returns the value of the first element. So, be sure to remember that these four methods have a return value.   In addition, the Array.splice () method also has a return value, the returned array containing the deleted elements (such as the fruit of the element is deleted, spicy returns an empty array):
1<script>2 varstr = ' John ';3 varstr2 = ' Angus ';4 varA = Str.split (' ');5 varb = str2.split (' ');6 varc = A.splice (2,1, "test"));7 8Console.log (a);//Array ["J", "O", "Test", "N"]9Console.log (b);//Array ["A", "N", "G", "U", "S"]TenConsole.log (c);//Array ["H"] One</script>

Ps+ps:  also mention that the reverse (), sort () actions affect the original array:
 1  <script>2  var  str = ' john ' ;  3  var  a = Str.split (' ); 4  var  C = 5  // array ["H", "J", "N", "O"]  7  Console.log (a); // array ["H", "J", "N", "O"]  8  </script> 

The concat () and slice () operations do not affect the array .... (Speaking of the Concat () method, think of the method of efficiently replicating arrays that you saw yesterday:var newArr = arr.slice (0) and var newArr = Arr.concat (), write it down to spare ... Well, there's also the website: http://www.jb51.net/article/5769.htm)

The First blog post for beginners, I am the front-end "enthusiast" of the two force ...
 

Kneeling in a certain degree of Autumn recruit the Web front-end written questions

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.