自我执行函数在条件之后传递对象 [英] Self executing function passing object after condition

查看:115
本文介绍了自我执行函数在条件之后传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个自执行函数,它在声明包含var的条件下执行,如果它不存在则传递一个对象。

I have come across a self executing function that executes on a condition that the declared containing var exists, and if it doesn't exist it is passed an object.

示例:

var myFunc = (function(myFunc){}(myFunc || {}));

为什么会有一个传递对象的或条件运算符?

How come there is an "or" condition operator that passes an object ?

推荐答案

var myFunc = (function(myFunc){}(myFunc||{}));

这没有任何意义,因为myFunc Argument总是 { } - 我对此感到困惑。

This doesn't make any sense because the myFunc Argument will always be {} - I'm confused by that.

我会解释一个


第一个例子

First Example



var cool = {
   person: 'john'
};

(function( Argument ){
  console.log( Argument ); // Result Object {person: "john"} 
}( cool || {} ));

在此示例中,定义了cool并且是一个对象,因此它不会超过 ||

In this example cool is defined and is a object so it wont go past the ||


下一个例子

Next example



var cool;
(function( Argument ){
  console.log( Argument ); // Result Object {}
}( cool || {} ));

在这个例子中,酷被定义为但是变量的defualt值是 undefined
所以在这种情况下它是未定义的,所以Argument是一个Object而不是

In this example cool is defined But the defualt value for a variable is undefined so in this case it is undefined so Argument is a Object instead

这篇关于自我执行函数在条件之后传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆