0

Hello all i am working with javascript and jsplumb library I am stuck in a problem and need help of experts.

Here is my scenario.

I have function in which i am using jsplumb library to create connection.

createlink = function (arg1, arg2 , con) {
  // doing some thing ;
    con = jsPlumb.connect({
             source: arg1,
             target: arg2
             });
 // doing some thing   
}

But the problem is that I want to access the same con created in the creatlink() function out side that function so what I did is

/*calling the function by passing the con as refrence*/
var con;
createlink("a", "b", con);
con.setParameter('name', "mycon"); // error as con is undefined

I have read that in JS objects can be passed as out param or pass by reference.

So what should be the correct and proper wayto access my con out side the function I don't want to return it as as this will create a separate copy.

Thanks any help will be appreciated.

A_user
  • 2,087
  • 6
  • 25
  • 33
  • 6
    Take the `var` off of `con` in `createLink` or replace `var con` with `return` and don't bother passing a ref param at all. – asawyer Nov 27 '12 at 18:45
  • 3
    ... as in `con = jsPlumb.connect({...});` with no `var` – Michael Berkowski Nov 27 '12 at 18:46
  • thanks alot for your reply. I have already tried it . i.e already removed the var. but its not working. Also can not return it as i am doing other things after jsplumb.connect ({....}) call – A_user Nov 27 '12 at 18:49
  • don't bother to change your question, even there are something, you can still cache the result, and return it at last. if you really want argument by reference, wrap your variable in an `object` or `array`. – xiaoyi Nov 27 '12 at 18:49
  • thanks for reply . But without pass con as param and not putting var before con will make con globle. Am i right ?? – A_user Nov 27 '12 at 18:54
  • Objects are passed by reference... but the references themselves are passed by value (most reference-based languages do this). So, if you pass a `con` parameter to `createlink`, then setting `con` from within `createlink` will change the object that the parameter variable refers to, but not the object that the passed-in `con` refers to (`undefined` in this case). As others have mentioned, either return the value or update a *field* of `con` (e.g. `con.out = ...` and call it with `var con = { }; createlink('a', 'b', con);`). Note that if you do `var con` within `createlink` that's a *third* con – Cameron Nov 27 '12 at 18:55
  • thanks @Cameron. Okay so you means that if i do this all the end of my createlink () that is return con. and do this var con1 = createlink. Now will con1 and con will have same value and changing one of them will make changes in the other ?? – A_user Nov 27 '12 at 19:03
  • @Cameron: No, "objects" are not values in JavaScript, and cannot be passed. The values you are dealing with is references. – newacct Nov 27 '12 at 20:25
  • @newact: Yes, that's what I said ('the references themselves are passed by value'). – Cameron Nov 27 '12 at 21:49

1 Answers1

1

You could return the connection object from the function:

createlink = function (arg1, arg2) {
    return jsPlumb.connect({
        source: arg1,
        target: arg2
    });
}

var con = createlink("a", "b");
jbabey
  • 45,965
  • 12
  • 71
  • 94