自调用函数写法

<html>
<script>
(function test(){
  alert(1+2);
})();
</script>
</html>


比如

 var i=0;
    (function next() {
        if(i>=newUsers.length){
            console.log("成功导入用户:"+newUsers.length+"条"); 
            return;
        }
        var newEmail = newUsers[i];
        Email.exists({ email: newEmail}, function (err, exists) {
            if(!exists){
                Email.create([
                    {
                        email: newEmail
                    } 
                ], function (err, items) { 
                    if(err!=null){
                        console.log({email:newEmail,error:err});
                    }else{
                        console.log({email:newEmail,"imported":true});
                    }
                    i+=1;
                    next();
                });
            }else{
                console.log({email:newEmail,"existed":true});
                i+=1;
                next();
            } 
        });
    })();



你可能感兴趣的:(JavaScript,函数)