Unknown js syntax / features

  1. jQuery wraps it’s entire code base in:

(function( window, undefined ) {
  
})(window);

Is this used so you can define any functions inside there without littering up the global namespace?
Then explicitly expose parts you want with window.xxx = yyy; ?

What is the code precisely doing?
It looks as if you can pass any number of objects into it - I’m not sure what the (window) at the end is doing.


2. prototype keyword:
is the prototype keyword’s sole purpose to add functions to all instances of existing objects?


Car = function(make) {
	this.make = make;
}
var honda = new Car('honda');
Car.prototype.numberOfWheels = 4;
alert(honda.numberOfWheels);

Can’t you just add all the properties to an object before you create instances of them? Why is it necessary to add this dynamically after initialisation?
3. multiple = assignments:
There’s code like this used throughout:


jQuery.fn = jQuery.prototype = {}

It seems pretty straight-forward but wanted to check I had understood correctly, all variables on the left of the last assignment get the lasts value.


var one = 1;
var two = 2;
var three = 3;
var x = one = two = three;
alert(one);
alert(two);
alert(three);
alert(x);

Thanks,

All of those could have very easily been Googled, you know?

Cheers,
D.

<sarcasm>Thanks for your help</sarcasm>

I’m not sure what help you think you need.

  1. You’re actually correct, but it’s extremely easy to test that assumption anyway:
(function(arg) {
  var test = arg;
  alert(test); // prints 'robots'
})('robots');

alert(test); // prints nothing
  1. Google “javascript prototype”.
  2. You weren’t even asking a question.

Cheers,
D.

It’s just different to any other syntax to what I have seen - they weren’t stupid questions.

The second set of brackets () call the function inside the first set.

Thanks,

I’m not saying they’re stupid questions; just that you were probably better off asking a search engine than a forum. It would have been a lot quicker than waiting for somebody to reply.

Cheers,
D.