www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: [RESOLVED] shared variable reference

  1. #1
    Join Date
    Jul 2010
    Posts
    135

    resolved [RESOLVED] shared variable reference

    I have a function in which I use a shared variable myFuncName.pair . I need the pair is accessible from myFuncName() but I don't want to declare it as a global variable outside the function.

    Code:
    function myFuncName(){
    var myFuncName.pair = { "val": { 1:null,2:null}, 
                     "id": { 1:null,2:null}
              };
    // now I start to work with myFuncName.pair
    //    var pair = myFuncName.pair;
        myFuncName.pair = { "val": { 1:null,2:null}, 
                     "id": { 1:null,2:null},
                     "ext":{ 1:null,2:null} };
        myFuncName.pair.id[1] = arguments[3].id;
        myFuncName.pair.id[2] = myFuncName.pair.id[1].replace(/(\w{2}\d)(\d)/,
            function (f,p,d){ return p + ((d==1)?'2':'1'); });
        myFuncName.pair.val[1] = $("input#"+myFuncName.pair.id[1],"fieldset#ext").val();
        myFuncName.pair.val[2] = $("input#"+myFuncName.pair.id[2],"fieldset#ext").val();
        console.log(pair.val);
        if (myFuncName.pair.val[1] && myFuncName.pair.val[2]) {
            myFuncName.pair.ext[1]=pair.val[1]+","+pair.val[2];
            myFuncName.pair.ext[2]=pair.val[2]+","+pair.val[1];
           } else pair.ext[1]=pair.val[1];
    }
    But It seems to me the code is somewhat longer that is it necessary..Is possible to create some reference to myFuncName.pair
    and save it to pair variable... so the command

    Code:
    pair = something;
    will save value to myFuncName.pair... (Is it possible?)

    In the result the code should be more simple not using directly myFuncName.pair but just a reference to it (just pair).

    Code:
        if (keyword!="restart&FilterCut") {
        var pair = filtrujVzory.pair; // but this line is not good I think, I know the next line will overwrite it and set it to local variable.
        pair = { "val": { 1:null,2:null}, 
                     "id": { 1:null,2:null},
                     "ext":{ 1:null,2:null} };
        pair.id[1] = arguments[3].id;
        pair.id[2] = pair.id[1].replace(/(\w{2}\d)(\d)/,
            function (f,p,d){ return p + ((d==1)?'2':'1'); });
        pair.val[1] = $("input#"+pair.id[1],"fieldset#ext").val();
        pair.val[2] = $("input#"+pair.id[2],"fieldset#ext").val();
        console.log(pair.val);
        if (pair.val[1] && pair.val[2]) {
            pair.ext[1]=pair.val[1]+","+pair.val[2];
            pair.ext[2]=pair.val[2]+","+pair.val[1];
           } else pair.ext[1]=pair.val[1];

  2. #2
    Join Date
    Aug 2010
    Location
    Baltimore, Maryland, United States
    Posts
    138
    Give the function a local variable:
    Code:
    function myFuncName(somethingHere)

  3. #3
    Join Date
    Jul 2010
    Posts
    135
    Quote Originally Posted by Ofekmeister View Post
    Give the function a local variable:
    Code:
    function myFuncName(somethingHere)
    ???

    Man, the object is saved in property "pair" in myFuncName. It is not necessary to use arguments.
    Last edited by crazy boy; 09-02-2010 at 04:18 PM.

  4. #4
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Hi,

    There are several ways to accomplish what you're trying to do, and sometimes it isn't clear what the best techniques are. My recommendation is use of the pattern known as parasitic inheritance. Check out the YUI Theater (watch all of Douglas Crockford's videos) to get an idea of what I'm referring to. He goes into enough detail that you should be able to create shared, truly private (yes, in JavaScript) variable using lexical scoping and object referencing.

    Of course, after you've watched the videos and tried for yourself, if you still have questions, please feel free to ask (I realize these concepts can be quite advanced for the average JavaScript developer). The future of JavaScript, and in fact many languages including Java, is in this pattern of programming.
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  5. #5
    Join Date
    Jul 2010
    Posts
    135
    I've seen 1st three videos but in the 3rd one I have problem with his examples. I did not understand much what he talks about because he uses such hard examples. Even I read his article about curring

    http://javascript.crockford.com/www_...ipt/index.html

    was unable to understand. 1st part about ML OK. Then I looked for some stuff and in Wikipedia I understand the mathematical stuff of curring. But here other problems with incomplete examples (what the hell means the php stuff in JS code)...
    http://www.dustindiaz.com/javascript-curry/

    Do you know some better links where would be really simple examples? I am now staying at the lesson 3 but I will continue then.

  6. #6
    Join Date
    Jul 2010
    Posts
    135

    Thumbs up found it

    I found very good article, very simple and very well explained:
    http://www.webreference.com/programm...vascript/rg17/
    Having my recommendations for those who are interested!

  7. #7
    Join Date
    Jul 2010
    Posts
    135
    Jona,
    I studied the stuff from Douglas Crockford and others about curry, partial applications and classical and parasitic inheritance but I don't know where you wanted to lead me.

    DC write here
    Code:
    function ZParenizor2(value) {
        var that = new Parenizor(value);
        that.toString = function () {
            if (this.getValue()) {
                return this.uber('toString');
            }
            return "-0-"
        };
        return that;
    }
    Example of parasitic inheritance.
    But I don't know how to use it to make the short reference to myFuncName.pair object.

    Did you mean to define something like this?
    Code:
    function myFuncName(){
    var myFuncName.pair = { "val": { 1:null,2:null}, 
                     "id": { 1:null,2:null}
    mp = new myFuncName.pair;
    That would create new instance and I just want to improve (shorten) access to the long refference "myFuncName.pair"

    Other thing ... mp in this case is not global but local variable, would it have access to myFuncName.pair from global?
    Last edited by crazy boy; 09-13-2010 at 10:10 AM.

  8. #8
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    I'm not sure I understand the question. If you just want to shorten the name of the variable, you can create a local variable that references it.

    JavaScript's scoping is lexical, which means that the deeper (more localized) your variables become, the more localities they have access to. Thus, a variable in the global scope can always be accessed by a variable in a localized scope, and furthermore a variable in a localized scope can be accessed by a variable in a sublocal scope.
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  9. #9
    Join Date
    Jul 2010
    Posts
    135
    I will return back to this thread later, now I need to solve closure problem... creating new thread... so I cannot to test this thread now because of some chaos in the code.

  10. #10
    Join Date
    Jul 2010
    Posts
    135
    Code:
    function myFuncName(){
    var myFuncName.pair = { "val": { 1:null,2:null}, 
                     "id": { 1:null,2:null}
    mp = new myFuncName.pair;
    
    /* some code here works with pm object. pm. Servers as shortcut to
       myFuncName.pair .... mp object is changed.
    */
    myFuncName.pair = mp; // I set global function property to new value
    mp = null; // I don't need mp more

  11. #11
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Hi,

    I'm exactly not sure what the question is; can you elaborate, please?
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  12. #12
    Join Date
    Jul 2010
    Posts
    135
    I've solved this question. I don't know how to say it simple. In the 1st code here you see I use this type of access: "myFuncName.pair" as an access to all properties. I wanted to shorten the code not to use such long name, but just a refference to "myFuncName.pair". In the last code of mine you see I use mp instance to get to "myFuncName.pair".

  13. #13
    Join Date
    Jul 2010
    Posts
    135
    Hey, I thought this question was already resolved. I think this part of code works and now I got to know that does not. I got an error:

    Code:
    pair = new filtrPatterns.pair
    Pair filtrPatterns.pair ... is not constructor.

    filtrPatterns.pair is object, so do I need to do a function from it? Here is my code:

    Code:
    function filtrPatterns(sg21_t, sg22_t){
    
        var getPair = function (){
          if (keyword!="restart&FilterCut") {
          // pair = filtrPatterns.pair;
          if (!filtrPatterns.pair) {
              filtrPatterns.pair = { "val": { 1:null,2:null}, 
               "id": { 1:null,2:null},
               "suffix":{ 1:null,2:null} };
              pair = new filtrPatterns.pair;
          }
          pair.id[1] = arguments[3].id;
          pair.id[2] = pair.id[1].replace(/(\w{2}\d)(\d)/,
              function (f,p,d){ return p + ((d==1)?'2':'1'); });
          pair.val[1] = $("input#"+pair.id[1],"fieldset#ext").val();
          pair.val[2] = $("input#"+pair.id[2],"fieldset#ext").val();
          if (pair.val[1] && pair.val[2]) {
              pair.suffix[1]=pair.val[1]+","+pair.val[2];
              pair.suffix[2]=pair.val[2]+","+pair.val[1];
             } else pair.suffix[1]=pair.val[1];
          filtrPatterns.pair = pair;
           }
          }
    .... /// main code of parent function
    }
    I just want to ask you if this attempt to repair is correct:

    Code:
        var getPair = function (){
          if (keyword!="restart&FilterCut") {
          if (!filtrPatterns.pair) {
              filtrPatterns.pair = function (){ "val": { 1:null,2:null}, 
               "id": { 1:null,2:null},
               "suffix":{ 1:null,2:null} };
              pair = new filtrPatterns.pair;
          }
          pair.id[1] = arguments[3].id;
          pair.id[2] = pair.id[1].replace(/(\w{2}\d)(\d)/,
              function (f,p,d){ return p + ((d==1)?'2':'1'); });
          pair.val[1] = $("input#"+pair.id[1],"fieldset#ext").val();
          pair.val[2] = $("input#"+pair.id[2],"fieldset#ext").val();
          if (pair.val[1] && pair.val[2]) {
              pair.suffix[1]=pair.val[1]+","+pair.val[2];
              pair.suffix[2]=pair.val[2]+","+pair.val[1];
             } else pair.suffix[1]=pair.val[1];
          filtrPatterns.pair = pair;
           }
          }
    but this is not constructor still, because function cannot have syntax:
    function(){property:value}

    THE GOAL OF THE WHOLE THING:

    I just want to ask you if this attempt to repair is correct:

    The only reason for creating a instance is that I want to shorten the access.
    Not to use syntax like so:
    Code:
    filtrPatterns.pair.suffix[1]=filtrPatterns.pair.val[1]+","+filtrPatterns.pair.val[2];
    but I want to use this short syntax:
    Code:
    pair.suffix[1]=pair.val[1]+","+pair.val[2];
    How to solve this if I want to short used reference but I don"t know if I can use a function as a constructor...?
    Last edited by crazy boy; 10-16-2010 at 06:23 AM.

  14. #14
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    If you want to shorten the reference, use this:

    Code:
    var pair = filtrPatterns.pair;
     pair.suffix[1] = pair.val[1]+','+pair.val[2];
    In order to make your code above work, you're close, but you need to make the function return an object, or specify a property of the function. Note that this will clone the object rather than creating a reference to it.

    Code:
        var getPair = function (){
          if (keyword!="restart&FilterCut") {
          if (!filtrPatterns.pair) {
              filtrPatterns.pair = function (){ return {"val": { 1:null,2:null}, 
               "id": { 1:null,2:null},
               "suffix":{ 1:null,2:null}}; };
              pair = new filtrPatterns.pair();
          }
          pair.id[1] = arguments[3].id;
          pair.id[2] = pair.id[1].replace(/(\w{2}\d)(\d)/,
              function (f,p,d){ return p + ((d==1)?'2':'1'); });
          pair.val[1] = $("input#"+pair.id[1],"fieldset#ext").val();
          pair.val[2] = $("input#"+pair.id[2],"fieldset#ext").val();
          if (pair.val[1] && pair.val[2]) {
              pair.suffix[1]=pair.val[1]+","+pair.val[2];
              pair.suffix[2]=pair.val[2]+","+pair.val[1];
             } else pair.suffix[1]=pair.val[1];
          filtrPatterns.pair = pair;
           }
          }
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  15. #15
    Join Date
    Jul 2010
    Posts
    135

    Thumbs up

    Thanx Jona, this is brilliant. You deserve 5 stars.

    I still have some problems (suddenly) with this code, but I need to struggle with it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles