Home  >  Article  >  Web Front-end  >  javascript closure question_javascript skills

javascript closure question_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:12:56968browse
Copy code The code is as follows:

var test=(function() { var arr2=[1,2 ,3,3]; return function(){ return arr2; }; })()
Array.prototype.f1= function () { return []; }
Array.prototype.f2= function () { this.length=0; return this; }  

Then call it in two ways:
One: var arr= test();
console. log(arr.length); the result is 4
arr.f1();
arr= test();
console.log(arr.length); the result is 4
This is easy to understand;

Two: var arr= test();
console.log(arr.length); the result is 4
arr.f2();
arr= test();
console.log(arr.length); the result is 0
I don’t know why this is happening. Could it be that arr.f2() can modify arr2 in the closure in the test function?
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