JS return function

function firstLoad() {
	var _list=[];
	return function(id) {
		if(_list.indexOf(id)>=0){
			return false;
		}else {
			_list.push(id);
			console.log(_list);
		}
	}
}
var first=firstLoad();
first(10);
first(10);
first(11);

在这个地方为啥要return function?为啥去掉return之后会报错?

retisan
浏览 1504回答 1
1回答

李晓健

因为 var first=firstLoad();   就是得到firstLoad 这个方法的返回值,然后赋给 first ;   也就是这个first就是 方法firstLoad 的返回值,如果你不加return ,方法默认传回的就是undefined; 你后面的这三行就是执行一个方法 first(10);first(10);first(11); 没有返回 frist就是undefined,它不是一个方法,就不能执行,就报错了。

慕瓜9220888

UBHZHYKCWNBRBCZGXNJKCDZWIVVOIEDSJHQTARACDQKTPOFKPBRYKKHSKCURUAXRCDOJTWGKOCUTFMYYMSJOQPCFGDNVQPREXHKVWVRWPPSYZXWLRNXKDGRQRDQQTJVMDVESPZAIFRTBOEROIWXWLIUKRIDPEGTJFNPKZOQDWMIAVXMYEUXHFWPKAGTNQTEQZAXUKDQXVLUHNDGAITBHNAHEUFUBLBOUKWOLXRPMQKUTIIXJDHXHFVUTXURHBTCUDHURINJFYHLIOXKYNDFJGQAGFOATDVWWFNDFJOOYWTYQGPWLIMLZSBAEEEFDCENMQDEUEDLQZWXEERSLSLSTAAVKWXWKHRRPNNILIPWWYHTIQNGGCKETFXENKXBCUTVSVRNLKJIRYOVGPEZXJGFIIYGBXYLVRZMUWOBBLENITVDCRUSRTKDFLYIBXDEVNZWISPWAHZBOIBXARTCAHQDGWTVHWJVFPXBACHRHXXJKZASMZSSDEDDVTJDOGHWYTQBEEHOGSXCJPLHFUGJEHHDTKFXJTPCCFQONIIEEYWNZUPCFFPLPEACYOBUSZUZRLEXXHEWIHUGGBXIXJHXNKMLCCONKLLKLFYAQQJWTDGADTHLKRXIKGZYFLVCXCLHQTDTLMHQRYBNXVTVMKNTGAVWQMUUDAACMPNPILURHOECEXXCLTEWOXGJTZLDYQMVPOJKIOTDWPFIESBQRHANQUSEJDNUTGQNMVRJAXHRV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