Home  >  Article  >  Web Front-end  >  JavaScript interview questions from qq_javascript skills

JavaScript interview questions from qq_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:22:251409browse
Copy code The code is as follows:

var info="Tencent Paipai.com (www.paipai.com) It is a well-known e-commerce website owned by Tencent. ";
info ="Paipai.com was launched on September 12, 2005.";
info ="It was officially launched on March 13, 2006.";
info ="It is currently the second largest e-commerce platform in China.";
info=info.split(",");
for(var i=0; i{
alert(info[i]);
}

This question seems like a toss-up at first, because later you have to separate each item with a comma and then alert each item. Why not construct an array? Object to store text content, and a temporary variable info is used to store
such as var info=["Tencent Paipai.com (www.paipai.com) is a well-known e-commerce website under Tencent.","Paipai.com in It was launched online on September 12, 2005, and officially launched on March 13, 2006. It is currently the second largest e-commerce platform in China. But then I thought that if it was optimization, this question would be meaningless.
Look carefully at the info variable and find that it adds a string every time. If the string is large and numerous, it will greatly affect the performance.

The string type in js is a basic type, so generally they are stored on the stack. If the string is large, info will become a very long string each time, which will be very slow

It would be much better if it is stored in a reference type array, such as
Copy code The code is as follows:

var temp=[];
temp.push("Tencent Paipai (www.paipai) .com) is a well-known e-commerce website owned by Tencent. ");
temp.push()....//temp is just a pointer to an array on the heap
....

The last trick is temp.join(""). This idea can be adopted for dealing with large string connection problems.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn