1

Since functions are first class objects, it should be possible to assign to the members of them.

Am I right thinking that arguments.callee does this?

Are there any other ways to set these fields?

How is it possible to set field in first case?

function something1() {
    arguments.callee.field = 12;
} 

alert(something1.field); // will show undefined
something1();
alert(something1.filed); // will show 12

something2 = function() {
    arguments.callee.field = 12;
};

alert(something2.field); // will show undefined
something2();
alert(something2.field); // will show 12

UPDATE 1

I mean how to access members from within function when it runs.

Dims
  • 47,675
  • 117
  • 331
  • 600

4 Answers4

1

Here's how I would do it:

function something1() {
    this.field = 12;
} 

var test = new something1();

alert(test.field); // alerts "12"
test.field = 42;
alert(test.field); // alerts "42"

If you're going to treat it like a class, you need to create a new instance of it before you can access its fields.

JSFiddle

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
1

You don't need to use arguments.callee to refer to a function that has a name; you can just use the name. This is naturally the case when you declare the function using the

function name(...) { ... }

syntax; but even in a function expression, you're allowed to supply a temporary name:

(function temp_name(...) { ... })(arg);

So, if you want to set the properties from inside the function, you can write:

function something1() {
    something1.field = 12;
} 

alert(something1.field); // will show undefined
something1();
alert(something1.field); // will show 12

something2 = function something2() { // note the second "something2"
    something2.field = 12;
};

alert(something2.field); // will show undefined
something2();
alert(something2.field); // will show 12
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Can I write `something2 = function something3() {...};` and what will be the difference between two names? – Dims Feb 01 '12 at 20:46
  • 1
    @Dims: Yes, you can. The difference is that the name `something3` is local to the function, whereas the name `something2` will have whatever outer scope you'd expect. So, for example, you might write `var f = function g() { alert(g); }; alert(f); f();` -- with `g` being used inside the function, and `f` being used afterward. – ruakh Feb 01 '12 at 21:12
1

Am I right thinking that arguments.callee does this?

Yes it did, but now they deprecated it.

Are there any other ways to set these fields?

The official way to replace callee is to use an explicit function name, as in funcName.propertyName=.... This is however not always convenient, for example, with dynamically generated functions. To quote John Resig, we're going to miss arguments.callee, it was quite useful for specific tasks.

georg
  • 211,518
  • 52
  • 313
  • 390
0

Simple

function something1() {};
something1.Property = "Foo";

You can directly assign the properties to any function just like a normal object. If to say in OOP language create static properties and methods.

Edit

The same inside the function

function something1() {
     something1.AnotherProp = "Bar";
};
something1.Property = "Foo";
Oybek
  • 7,016
  • 5
  • 29
  • 49