Home > Web Front-end > JS Tutorial > javascript closure question_javascript skills

javascript closure question_javascript skills

WBOY
Release: 2016-05-16 18:12:56
Original
1020 people have browsed it
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?
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template