Use code from another script

Discussion in 'Javascript' started by Archos, Feb 7, 2012.

  1. Archos

    Archos Guest

    I need to write code in one script and reference it from another. So
    I've tried the next code but it doesn't works like I was expecting

    // ==
    var g = {};

    (function() {
    function Test(a, b) {
    this.a=a;
    this.b=b;
    }

    g[Test] = Test;
    })();
    // ==

    >>> g.Test(1,2);

    TypeError: g.M is not a function

    What's wrong?
     
    Archos, Feb 7, 2012
    #1
    1. Advertisements

  2. Archos

    Scott Sauyet Guest

    Archos wrote:
    > I need to write code in one script and reference it from another. So
    > I've tried the next code but it doesn't works like I was expecting
    >
    > var g = {};
    >
    > (function() {
    > function Test(a, b) {
    >         this.a=a;
    >         this.b=b;
    > }
    > g[Test] = Test;

    ^^^^^^^
    > })();
    >
    > g.Test(1,2); // TypeError


    > [ ... ]
    > What's wrong?


    The line above should read either:

    | g.Test = Test;

    or

    | g["Test"] = Test;

    The code you wrote assigned a property value of the function
    correctly, but the property name you assigned is what your
    implementation determines the string value of your `Test` function
    should be.

    But there's still something else wrong. Your function seems to be a
    constructor function (assigning properties to `this` and having an
    initial capital letter) but when you call it, you don't use `new`. As
    corrected above, the code would result in `g` having the properties
    `Test`, which points to the function, `a` with a value of 1, and `b`
    with a value of 2. Is that what you wanted? Or did you want to do
    something like

    | var something = new g.Test(1, 2);

    In this version, `something` now looks like {a: 1, b: 2}, although it
    has other properties inherited from Object as well.

    What are you trying to accomplish?

    -- Scott
     
    Scott Sauyet, Feb 7, 2012
    #2
    1. Advertisements

  3. Archos

    Archos Guest

    On Feb 7, 12:45 pm, Scott Sauyet <> wrote:
    > Or did you want to do something like
    >
    > |    var something = new g.Test(1, 2);
    >
    > In this version, `something` now looks like {a: 1, b: 2}, although it
    > has other properties inherited from Object as well.
    >
    > What are you trying to accomplish?
    >

    It's just what I want, thanks.
    I'm trying to simulate a library, which has constructor functions,
    with its properties and prototypes, to be used easily from another
    scripts.
     
    Archos, Feb 7, 2012
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wet Basement
    Replies:
    1
    Views:
    2,894
    BobMonk
    Jul 15, 2003
  2. Chuck
    Replies:
    2
    Views:
    837
    Chuck
    Nov 12, 2004
  3. keithb
    Replies:
    1
    Views:
    1,240
    Bruce Barker
    Mar 29, 2006
  4. sarmin kho
    Replies:
    1
    Views:
    567
    Cameron Laird
    Jun 4, 2004
  5. Replies:
    5
    Views:
    657
  6. Rajat
    Replies:
    3
    Views:
    1,032
    Jorgen Grahn
    Jan 8, 2010
  7. VYAS ASHISH M-NTB837
    Replies:
    2
    Views:
    800
    Jan Kaliszewski
    Jan 7, 2010
  8. Robert Cohen
    Replies:
    3
    Views:
    648
    Andrew Durstewitz
    Jul 15, 2003
Loading...