如何理解js中temp = ( function(){ return 'abc' } )()

本文解释了如何使用JavaScript中的匿名函数及立即执行函数表达式(IIFE)。通过具体示例介绍了如何将函数运行结果赋值给变量,并探讨了匿名函数的优势,如避免命名污染和减少内存泄漏。

如何理解temp = ( function(){ return 'abc' } )() 

其实就是把运行结果返回给前面这个变量。

例如: 
temp = ( function(){ return 'abc' } )()
其运行结果就是temp='abc',

相当于:
function fun()
{
    return 'abc';


temp = fun();

alert(temp);

弹出来的结果就是abc。

 

 

第二个括号也可以放置参数,例如:

  var temp=(function(str){alert(str);return str;})('出来!');
  alert(temp);

将会弹出两个“出来!”的结果,其中后面这个括号里的是传入第一个括号里函数的参数,

当然在这个函数中如果没有“return str;”这个语句,变量temp是没有值的。

相当于:

var temp=fun('出来!');
 function fun(str){
  alert(str);
  return str;
 }

 

 

 

(function(){var i=100;alert(i)})()
这种写法就是传说中的匿名函数,它的好处是函数内部定义的对象在函数外面永远无法访问,除此之外这个匿名函数也是不可被其它代码访问的,即使得对象之间不 容易被命名污染(在 js中很多错误是由于对象命名冲突引起的)。按照通常的写法我们会这样写function a(){var i = 100;alert(i)};a();这样写就留下了一个对象a(在不要再用的时候就成了内存垃圾)。

matches = completion_matches(text, filename_completion_function); 这个补全路径只会补全一个匹配项 比如当前目录下有个etc目录,然后etc里有1.txt文件、abc子目录,然后abc中还有个2.txt 现在是我输入dir e,按tab键后补全成dir etc/ ,连续按tab键后还是显示dir etc/ 我想要的效果是:连续按tab键后,能循环显示出:dir etc/ dir etc/1.txt dir etc/abc/ dir etc/ 这样循环显示 然后如果我输入了dir etc/a,然后连续按tab键补全,则循环显示:dir etc/abc/ dir etc/abc/2.txt /* * return first found file name starting by the ``text'' or NULL if no * such file can be found * value of ``state'' is ignored * * it's caller's responsibility to free returned string */ char* filename_completion_function(const char* text, int state) { static DIR* dir = NULL; static char * filename = NULL, *dirname = NULL; static size_t filename_len = 0; struct dirent* entry; char* temp; size_t len; if (state == 0 || dir == NULL) { if (dir != NULL) { closedir(dir); dir = NULL; } temp = strrchr(text, '/'); if (temp) { temp++; filename = XREALLOC(MTYPE_TMP, filename, strlen(temp) + 1); (void)strcpy(filename, temp); len = temp - text; /* including last slash */ dirname = XREALLOC(MTYPE_TMP, dirname, len + 1); (void)strncpy(dirname, text, len); dirname[len] = '\0'; } else { filename = XSTRDUP(0, text); dirname = NULL; } /* support for ``~user'' syntax */ if (dirname && *dirname == '~') { temp = tilde_expand(dirname); dirname = XREALLOC(MTYPE_TMP, dirname, strlen(temp) + 1); (void)strcpy(dirname, temp); /* safe */ XFREE(0, temp); /* no longer needed */ } /* will be used in cycle */ filename_len = strlen(filename); if (filename_len == 0) return (NULL); /* no expansion possible */ dir = opendir(dirname ? dirname : "."); if (!dir) return (NULL); /* cannot open the directory */ } /* find the match */ while ((entry = readdir(dir)) != NULL) { /* otherwise, get first entry where first */ /* filename_len characters are equal */ if (entry->d_name[0] == filename[0] #if defined(__SVR4) || defined(__linux__) && strlen(entry->d_name) >= filename_len #else && entry->d_namlen >= filename_len #endif && strncmp(entry->d_name, filename, filename_len) == 0) break; } if (entry) { /* match found */ struct stat stbuf; #if defined(__SVR4) || defined(__linux__) len = strlen(entry->d_name) + #else len = entry->d_namlen + #endif ((dirname) ? strlen(dirname) : 0) + 1 + 1; temp = XMALLOC(0, len); (void)sprintf(temp, "%s%s", dirname ? dirname : "", entry->d_name); /* safe */ /* test, if it's directory */ if (stat(temp, &stbuf) == 0 && S_ISDIR(stbuf.st_mode)) strcat(temp, "/"); /* safe */ } else temp = NULL; return (temp); } 源码,这个接口是readline库中自带的函数,然后你帮我优化代码,在这个接口的基础上,实现循环显示
07-24
char* filename_completion_function(const char* text, int state) { static DIR* dir = NULL; static char * filename = NULL, *dirname = NULL; static size_t filename_len = 0; struct dirent* entry; char* temp; size_t len; if (state == 0 || dir == NULL) { if (dir != NULL) { closedir(dir); dir = NULL; } temp = strrchr(text, '/'); if (temp) { temp++; filename = XREALLOC(MTYPE_TMP, filename, strlen(temp) + 1); (void)strcpy(filename, temp); len = temp - text; /* including last slash */ dirname = XREALLOC(MTYPE_TMP, dirname, len + 1); (void)strncpy(dirname, text, len); dirname[len] = '\0'; } else { filename = XSTRDUP(0, text); dirname = NULL; } /* support for ``~user'' syntax */ if (dirname && *dirname == '~') { temp = tilde_expand(dirname); dirname = XREALLOC(MTYPE_TMP, dirname, strlen(temp) + 1); (void)strcpy(dirname, temp); /* safe */ XFREE(0, temp); /* no longer needed */ } /* will be used in cycle */ filename_len = strlen(filename); if (filename_len == 0) return (NULL); /* no expansion possible */ dir = opendir(dirname ? dirname : "."); if (!dir) return (NULL); /* cannot open the directory */ } /* find the match */ while ((entry = readdir(dir)) != NULL) { /* otherwise, get first entry where first */ /* filename_len characters are equal */ if (entry->d_name[0] == filename[0] #if defined(__SVR4) || defined(__linux__) && strlen(entry->d_name) >= filename_len #else && entry->d_namlen >= filename_len #endif && strncmp(entry->d_name, filename, filename_len) == 0) break; } if (entry) { /* match found */ struct stat stbuf; #if defined(__SVR4) || defined(__linux__) len = strlen(entry->d_name) + #else len = entry->d_namlen + #endif ((dirname) ? strlen(dirname) : 0) + 1 + 1; temp = XMALLOC(0, len); (void)sprintf(temp, "%s%s", dirname ? dirname : "", entry->d_name); /* safe */ /* test, if it's directory */ if (stat(temp, &stbuf) == 0 && S_ISDIR(stbuf.st_mode)) strcat(temp, "/"); /* safe */ } else temp = NULL; return (temp); } 这个是readline库中自带的函数,但是这个函数只会匹配一个目录,其中的while循环就会break; 比如当前目录下有个etc目录,然后etc里有1.txt文件、abc子目录,然后abc中还有个2.txt 现在是我输入dir e,按tab键后补全成dir etc/ ,连续按tab键后还是显示dir etc/ 我想要的效果是:连续按tab键后,能循环显示出:dir etc/ dir etc/1.txt dir etc/abc/ dir etc/ 这样循环显示 然后如果我输入了dir etc/a,然后连续按tab键补全,则循环显示:dir etc/abc/ dir etc/abc/2.txt 如何字啊这个函数的基础上实现?
07-24
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值