demo eleven
I don't know what you understood before.Now you can judge the output of the following code
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); }
Familiar with that, right, you can do what you want with a few changes
You can try the console yourself to see if it works.for (var i = 0; i < 5; i++) { var btn = document.createElement('button'); btn.appendChild(document.createTextNode('Button ' + i)); (function(i){ btn.addEventListener('click', function(){ console.log(i); }); })(i); document.body.appendChild(btn); }
demo twelve
So let's judge the following code
var arr1 = "john".split(''); j o h n var arr2 = arr1.reverse(); n h o j var arr3 = "jones".split(''); j o n e s arr2.push(arr3); console.log("array 1: length=" + arr1.length + " last=" + arr1.slice(-1)); console.log("array 2: length=" + arr2.length + " last=" + arr2.slice(-1));
There are two main points. The first is that the reverse() method changes the original array, that is, arr1 becomes n h o j
Another important point is that similar to the form a = [0,1,2,3], b = a, changing the value of b will change the value of a, but if b is reassigned, the value of b will not change
var a = [10,11,12] var b = a; b.push(55); console.log(b); console.log(a);
So what if this is the case?
Running the answer is obviousvar a = [10,11,12] var b = a; b = [1,2,5,8] console.log(b); console.log(a);
demo thirteen
Direct Up Code
console.log(1 + "2" + "2"); console.log(1 + +"2" + "2"); console.log(1 + -"1" + "2"); console.log(+"1" + "1" + "2"); console.log( "A" - "B" + "2"); console.log( "A" - "B" + 2);
Remember a few rules
1. If a number string is preceded by a'+','-', it will be converted to a number during the operation.
2. Returns NaN for strings that cannot be evaluated