Web Front End Questions finishing (program)

Source: Internet
Author: User

Tag: Call ash calls Ue4 type ONS length + + tool


Need web front-end course tool and ebook, can add: 33 group 105601600; 22 Group 120342833

1. var obj ={a:1}; (function (obj) {obj = {a:2};})       (obj); Q. does the value of obj change?

var obj = {a:1};

(function (obj) {

obj = {A:2};

}) (obj);

// Ask obj will the value change?

The external obj does not change.
Because the input parameter of obj in the anonymous function is equal to the creation of a local variable, obj, which points to a new object. If changed to (function () {obj = {a:2};})  (obj); It's going to change.


2. var obj = {a:1, func:function () {(function () {a=2;} (); }} ; does the value of a in Obj.func ()//obj change? in the anonymous function. This What's that pointing to? ?

var obj = {

A:1,

Func:function () {

(function () {

a=2;

}();

}

} ;

Obj.func ();

//obj in a will the value change? ? in the anonymous function. This What's that pointing to? ?

The A in obj does not change.  The This in the anonymous function points to the Global object window. This is equivalent to adding a property named A to window.
To change the value of a in obj, you should:

(function () {

THIS.A = 2

}). Call (this);

or as defined in obj

Func:function () {

var = this;

(function () {

self.a=2;

})();}

3. to implement functions within the function every 5 seconds to call themselves this function, after the first stop, how to do ?

(function () {

var index = 0;

function fn () {

if (Index < 100) {

index++;

SetTimeout (function () {

FN ();

},5000);

}

}

FN ();

})();

4. Click on a ul five li elements, respectively pop their serial number, how to do?
Method 1:

var oLi = document.getelementsbytagname (' li ');

for (var i=0; i<olis.length; i++) {

Olis[i].onclick = (function (j) {

Returnfunction () {

Alert (j);

}

}) (i);

}

Method 2:

var oLi = document.getelementsbytagname (' li ');

for (var i=0; i<oli.length; i++) {

(function (j) {

oli[j].onclick= function () {

Alert (j);

};

}) (i);

}

Method 3:

var oLi = document.getelementsbytagname (' li ');

for (var i=0; i<oli.length; i++) {

Oli[i].index = i;

Oli[i].onclick =function () {

alert (This.index);

}

}

5. JS Implementation of array de-weight how to implement ?
Method 1. Create a new temporary array to hold the existing elements in the array

var a = new Array (1,2,2,2,2,5,3,2,9,5,6,3);

Array.prototype.unique1 = function () {

var n = []; a new temporary array

for (var i=0;i<this.length; i++) {

        // If you put the current array I has been saved in a temporary array , so skip it.

if (N.indexof (this[i]) = =-1) {

N.push (This[i]);

}

}

return n;

}

Console.log (A.unique1 ());

Method 2. using a hash table to store existing elements

Array.prototype.unique2 = function () {

var hash = {},

n = []; Hash as a hash table , n as a temporary array

for (var i=0;i<this.length; i++) {

If (!hash[this[i]]) {// if There is no current item in the hash table

Hash[this[i]] = true; deposit hash Table

N.push (This[i]); the current element is push into the temporary array

}

}

return n;

}

Method 3. use IndexOf to determine if the position of the first occurrence of an array element is the current position

Array.prototype.unique3 = function () {

var n = [This[0]];

For (var i=1;i<this.length; i++)// start traversing from the second item

{

        // if the current array element appears in the array for the first time, the position is not I

        // description is a repeating element

if (This.indexof (this[i]) = = i) {

N.push (This[i]);

}

}

return n;

}

Method 4. First sort and then go to heavy

Array.prototype.unique4 = function () {

This.sort (function (A, b) {return a B;});

var n = [This[0]];

for (var i=1;i<this.length; i++) {

if (This[i]!=this[i-1]) {

N.push (This[i]);

}

}

return n;

}

The first and third methods both use indexof (), and the execution mechanism of the function iterates over the array
The second method uses a hash table, which is the fastest.
The third method also has a sort of complexity calculation.
Then we did a test, randomly generating 1 million 0-1000 array results as follows:

The third method is always nearly twice times the second method, and the fourth method is related to the range of the array,
If it is an array of 0-100

And if it is 0-10000, method four look at the effect is good

And the second method is always the best, but it's a space-changing time
The entire code is as follows

var a = [];

for (var i=0; i<1000000; i++) {

A.push (Math.ceil (Math.random () *10000));

}

Array.prototype.unique1 = function () {

var n = []; a new temporary array

for (var i=0;i<this.length; i++) {

        // If you put the current array I has been saved in a temporary array , so skip it.

if (N.indexof (this[i]) = =-1) {

N.push (This[i]);

}

}

return n;

}

Array.prototype.unique2 = function () {

var hash = {},

n = []; Hash as a hash table , n as a temporary array

for (var i=0;i<this.length; i++) {

If (!hash[this[i]]) {// if There is no current item in the hash table

Hash[this[i]] = true; deposit hash Table

N.push (This[i]); the current element is push into the temporary array

}

}

return n;

}

Array.prototype.unique3 = function () {

var n = [This[0]];

For (var i=1;i<this.length; i++)// start traversing from the second item

{

        // if the current array element appears in the array for the first time, the position is not I

        // description is a repeating element

if (This.indexof (this[i]) = = i) {

N.push (This[i]);

}

}

return n;

}

Array.prototype.unique4 = function () {

This.sort (function (A, b) {return a B;});

var n = [This[0]];

for (var i=1;i<this.length; i++) {

if (This[i]!=this[i-1]) {

N.push (This[i]);

}

}

return n;

}

var begin1 = new Date ();

A.unique1 ();

var end1 = new Date ();

var begin2 = new Date ();

A.unique2 ();

var end2 = new Date ();

var begin3 = new Date ();

A.unique3 ();

var end3 = new Date ();

var begin4 = new Date ();

A.unique4 ();

var end4 = new Date ();

console.log (" method One Execution time :" + (end1-begin1));

console.log (" method Two execution time :" + (end2-begin2));

console.log (" method Three execution time :" + (end3-begin3));

console.log (" method four execution time :" + (END4-BEGIN4));

"The content display is limited, can add the group to download, the group file will update the study material regularly, as well as practiced hand small case"

Web Front End Questions finishing (program)

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.