Question: I have created a custom newform.aspx by cloning the form generated by SharePoint Designer and added it to a VS2010 project. Everything seems to be working fine like setting the value etc... but I couldn't figure out how to set the `Required` property of a field.

Based on a certain condition I would like to set the required property of a field to true or false.

I am accessing the controls using `CompositeField` and `BaseFieldControl` but they dont seem to give a handle to set required property value.

Any pointers are greatly appreciated.

Answer: 
Its a self calling function, that invokes its self as the script finishes loading. You can call it without arguments, or you can add arguments to it such as `window` or `document`.

You use it in a way that jQuery use it:

```
(function( window, undefined ) {
    // jQuery code
})(window);

```

An (almost) alternative syntax to do the same thing:

```
! function( window, undefined ){
    // some code…
}(window);

```

Read more at: <http://sarfraznawaz.wordpress.com/2012/01/26/javascript-self-invoking-functions/>
The last set of parentheses causes the function to execute immediately. A function is created and executed without ever assigning it anywhere. The reason one might wrap their code in a function like this is to encapsulate code. Take this for example:

```
var myVar = 'whatever';
function shout() { alert(myVar); }

```

Here, `myVar` and `shout` have just become global variables. You can open up your console and type `window.myVar` or `window.shout` and you'll be able to access and change those variables. By wrapping it in a function, those variables remain local to the outer function:

```
(function() {
    var myVar = 'whatever';
    function shout() { alert(myVar); }
})();

```

`window.myVar` and `window.shout` are undefined. The only exist inside that function.

The pattern is also used to create a closure around local variables. See [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example).