Kryptic
Kryptic

Reputation: 1952

Access a function's property inside the function

I want to be able to assign a property to a function inside the function itself. I do not want to assign it to the object of invocation. So I want the equivalent of doing this:

var test  = function() {                    
    return true;         
};

test.a = 'property on a function';
alert(test.a);

Instead of this, where the property is assigned to a global object:

var testAgain = function() {
   this.a = "this property won't be assigned to the function";

   return true;  
};

testAgain();
alert(window.a);

Edit: To clarify, I'm wondering if there's something like this:

var test = function() {
   function.a = 'property on a function';
};
alert(test.a); // returns 'property on a function'

Without knowing that the function is called test or having to execute it. I know of course this isn't valid syntax

Upvotes: 1

Views: 159

Answers (4)

Lajos Arpad
Lajos Arpad

Reputation: 76436

var test = function() {
    test.a = 'a';
};

Or you can use prototypes, read more here.

Upvotes: 0

zzzzBov
zzzzBov

Reputation: 179046

[is there a way to set a property on a function] without knowing that the function is called test or having to execute it.

Emphasis mine.

You can set a property on a function without knowing what its global variable name is necessarily going to be, however you do have to have a reference to the function in one way or another.

The module pattern is as close of a fit as I can think of:

window.test = (function () {
    //the function could be named anything...
    function testFn() {
        ...code here...
    }
    //...so long as the same name is used here
    testFn.foo = 'bar';
    return testFn;
}());
window.test.foo; //'bar'

The outer closure prevents testFn from being accessed anywhere globally, so all other references will have to use window.test.


This part of the answer is associated with the prior version of the question.

The simplest way of doing this is to use a named function:

var test = function testFn() {
    testFn.foo = 'bar';
    return true;
};

test.foo; //undefined
test();
test.foo; //'bar'

A better way of doing this is to use the module pattern so that you don't accidentally create issues with global leakage:

var test = (function () {
    function ret() {
        ret.foo = 'bar';
        return true;
    }
    return ret;
}());

test.foo; //undefined
test();
test.foo; //'bar'

Upvotes: 3

Some Guy
Some Guy

Reputation: 16190

You can do this by simple using the name to assign the property like this:

var test = function () {
    test.a = 'a';
    return true;
};

When test is invoked, the property will be set.

Demo

You could use arguments.callee, as su- said, but that's considered really bad practice. Also, it won't work in strict mode.

Upvotes: 0

su-
su-

Reputation: 3166

var testAgain = function() {
    arguments.callee.a = "this property won't be assigned to the function";
    return true;  
};

testAgain();
alert(testAgain.a);​

Upvotes: 1

Related Questions