HILARUDEEN S ALLAUDEEN
HILARUDEEN S ALLAUDEEN

Reputation: 1752

Can I use function as an object inside the same function

I write the code as follows,

function Myfunction(){
 Myfunction.myvar = "somevar";
}

After I execute the function, I can able to access Myfunction.myvar

How is it working? And If I do so, What is the problem hidden in this?

If any problem, please explain context of that.

Upvotes: 8

Views: 180

Answers (4)

Sebastian Baltes
Sebastian Baltes

Reputation: 512

One problem you might get because of scoping, as a more complicate example shows:

function Other() {
    console.log("a) Other.Myvalue",Other.Myvalue);
    Other.Myvalue=typeof Other.Myvalue==='undefined' ? 0 : Other.Myvalue+1;
    console.log("b) Other.Myvalue",Other.Myvalue);
}
Other();
Other();

this would lead to

a) Other.Myvalue undefined
b) Other.Myvalue 0
a) Other.Myvalue 0
b) Other.Myvalue 1 

so you realy bind your variable Myvar to the function object itself which is a singleton and exists just once (and is created by the function definition itself in the current context which may be the global context), not to an instance of that object. But if you just use static values and don't need any logic, it's like the literal form, and I wouldn't expect any problems, other that the literal form is more convenient:

var Other = {
  Myvalue: "somevalue"
};

Upvotes: 2

mikach
mikach

Reputation: 2427

In javascript every function is an object. So you can add any custom fields to it.

function A() {}

A.somevar = 'this is custom field';

A.prototype.somevar2 = 'this is field in A proto';

So when you will create new object with A contructor it will take properties from prototype.

var b = new A();
alert(b.somevar2);

Upvotes: 0

James Allardice
James Allardice

Reputation: 165971

How is it working?

When you declare a function in some execution context, a binding is added to the variable environment of that context. When you reference an identifier, the current variable environment is checked to see if a binding exists for that identifier.

If no binding exists, the outer variable environment is checked, and so on, back up to the global scope.

So:

// OUTER SCOPE
// Binding exists for 'example'
function example() {
    // INNER SCOPE
    // No binding for 'example'

    // References 'example' in outer scope
    example.x = 1;
}

What is the problem hidden in this?

There are none (in general... although whether it's the right solution for you depends on what you're trying to do).

You are effectively creating a "static" property of the function. As JavaScript functions are first-class you can set properties on them as you would with any other object.


Note that the behaviour is different if you have a named function expression, rather than a function declaration:

var x = function example () {
    // Identifier 'example' is only in scope in here
};

Upvotes: 4

Thilo
Thilo

Reputation: 262534

Since the function does not execute until you call it, Myfunction.myvar is not evaluated immediately.

Once you call it, the function definition has been installed as Myfunction, so that it can be resolved when you do call it.

Something like the following would not work:

var x = {
       foo: 1,
       bar: x.foo } // x does not exist yet.

Upvotes: 5

Related Questions