PDA

View Full Version : JavaScript syntax question.



Elijah
15 Jun 2010, 5:03 AM
Ran across a snippet of code that left me and another developer wondering what exactly is this suppose to do. I can assume what its doing but it is not clear at all to me.



1. clearStatus : function(o){
2. o = o || {};
.......


So i take it that line 2. o is a global variable.
But what the heck is this? "o || {}"
If it were something like
o = (typeof o != 'undefined')?o:{};
that would make some sense to me but the syntaxt of the code at top I dont understand. Can someone clarify.

Animal
15 Jun 2010, 5:34 AM
Read it while understanding the operators: "o equals o or new Object"

So if o is truthy then o is set to reference o
else it references a new Object.

Elijah
15 Jun 2010, 5:59 AM
yeah I had just done a sample using that to see if it was really doing that. That format is very minute. Learn something everyday.

CrazyEnigma
15 Jun 2010, 6:20 AM
o is not a global variable. It is a pass parameter in line 1, the clearStatus function.

So in documentation when it says, clearStatus([o]), it is optional. Most parameters are optional and are not strictly typed. So to ensure that you don't get "o is undefined" error...

Elijah
15 Jun 2010, 6:42 AM
Yes line 1. has o passed in but on line 2. it reads to me as
the global o value is either equal to the parameter o or {}
if that is not the case then a better format to write it would be to have it as


clearStatus: function(argO){
var o = argO || {};

Why?
Because defining var o its scoped to the function.
Because defining your parameter as argO removes any confusion as to what your looking at in the code.

CrazyEnigma
15 Jun 2010, 7:02 AM
What you did is wasted space.

o is a global variable is completely false. It was not defined within the function. It was passed by the function call.

In order to use the parameter, you call:


var newObject = o;
You can also set the value, there is no way in javascript to prevent the object from changing.


changeObjectToArray: function (o) {
o = [];
}
So on the other side of the javascript call, you could get a return within the parameters. It is possible, but it is not standard programming practice, especially when your language such as javascript is dynamic. In other languages such as C#, there is an "out" and "ref" parameter that prevents the user from missing the changed parameter (Getting away from EXT JS now).



var someObject = {};
objectWithFnThatChangesSomeObject.changeObjectToArray(someObject);
alert(Ext.encode(someObject)); // []
If there is no reference to the object anywhere from within the function call or parameters list, it will create a global variable, (with exception to the "arguments" variable)

Animal
15 Jun 2010, 7:13 AM
I can see why beginners might want to create a new reference like that.

I taught a client recently who couldn't get his head around assigning



MyClass = function(config) {
config = Ext.apply({
default: value
}, config);


So we went with creating another local var "newConfig" to reference the result from Ext.apply and pass that to the superclass constructor.

As CrazyEnigma says, it's just a waste of precious Javascript opcode processing, and browsers don't have any spare to waste!

Elijah
15 Jun 2010, 7:29 AM
Okay,

just ran some simple examples and I see what is going on.

1. clearStatus : function(o){
2. o = o || {};
.......

Yes it is local.

I just never write code like that because had it been something like
1. clearStatus : function(o){
2. L = o || {};
.......
L would be global unless it were written as
1. clearStatus : function(o){
2. var L = o || {};
.......