Results 1 to 8 of 8

Thread: JavaScript syntax question.

  1. #1
    Ext JS Premium Member Elijah's Avatar
    Join Date
    Nov 2009
    Location
    USA
    Posts
    382

    Default JavaScript syntax question.

    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.

    Code:
    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.

  2. #2
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,890

    Default

    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.

  3. #3
    Ext JS Premium Member Elijah's Avatar
    Join Date
    Nov 2009
    Location
    USA
    Posts
    382

    Default

    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.

  4. #4
    Sencha User
    Join Date
    Sep 2009
    Location
    Calgary, Alberta, Canada
    Posts
    638

    Default

    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...

  5. #5
    Ext JS Premium Member Elijah's Avatar
    Join Date
    Nov 2009
    Location
    USA
    Posts
    382

    Default

    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
    Code:
    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.

  6. #6
    Sencha User
    Join Date
    Sep 2009
    Location
    Calgary, Alberta, Canada
    Posts
    638

    Default

    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:
    Code:
    var newObject = o;
    You can also set the value, there is no way in javascript to prevent the object from changing.
    Code:
    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).

    Code:
    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)

  7. #7
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,890

    Default

    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

    Code:
    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!

  8. #8
    Ext JS Premium Member Elijah's Avatar
    Join Date
    Nov 2009
    Location
    USA
    Posts
    382

    Default

    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 || {};
    .......

Similar Threads

  1. Aid for javascript syntax
    By wki01 in forum Ext 3.x: Help & Discussion
    Replies: 3
    Last Post: 24 Jun 2009, 5:29 AM
  2. Syntax question...
    By neall in forum Ext 2.x: Help & Discussion
    Replies: 3
    Last Post: 11 Jun 2008, 3:11 PM
  3. please help on javascript syntax problem
    By hygo in forum Ext 1.x: Help & Discussion
    Replies: 3
    Last Post: 21 Jun 2007, 11:23 AM
  4. Forum suggestion: Javascript syntax highlighting
    By JorisA in forum Community Discussion
    Replies: 2
    Last Post: 12 May 2007, 10:15 PM
  5. BorderLayout scope issues depending on Javascript OO Syntax
    By brett in forum Ext 1.x: Help & Discussion
    Replies: 11
    Last Post: 3 Feb 2007, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •