6

Why is it that I can do the following in javascript:

function a() {};
a.foo = 30;

Specifically, why can I set a property on the function a? After all, I cannot do this:

var a = 20;
a.foo = 30;
asked Sep 8, 2010 at 23:22

4 Answers 4

8

You really can't do this because it's a syntax error

function a = function() {};

I suppose you simply want to say:

function a() {}

Anyway. The reason that you cannot get a property out of a number, is that it is not a real Object.

a = 20;
a.foo = 30; // this works
alert(a.foo); // this alerts nothing

Believe it or not, the same goes for strings:

a = "ohai";
a.foo = 30; // this works
alert(a.foo); // this alerts nothing

However if it's String object, then it works as expected:

a = new String("ohai");
a.foo = 30; // this works
alert(a.foo); // this alerts 30

Or if it's an Number object. You get the point.

String and number literals are not objects in Javascript. That's the reason.

ManKum
2561 gold badge2 silver badges12 bronze badges
answered Sep 8, 2010 at 23:31
Sign up to request clarification or add additional context in comments.

Comments

4

In JavaScript the dot (.) operator expects it's left value to be an object. And in JavaScript, functions are objects.

Basically, in JavaScript, there are four main datatypes:

  1. Number
  2. String
  3. Boolean
  4. Object

Objects encompasses functions, arrays, date objects, and (for lack of a better word) regular objects. The function object is unique in that it contains executable code, and can be invoked.

Numbers are primitives, and thus you cannot access/assign properties to/from them.

answered Sep 8, 2010 at 23:45

2 Comments

there's Arrays too... they have different native prototypes than Objects (note that an Array will return a typeof as an object, but if you check its constructor you will see that it is in fact Array).
@Peter: Still, Arrays are objects. The ECMA standard defines "Array objects", "Function objects", ... and "Object objects"
3

In Javascript, a function is an object, so you can set properties on it:

function a() {};
a.foo = 30;
a.foo; // evaluates to 30

A number literal however creates a primitive value (which has no properties) and not an object.

a = 20; // Create primitive value using number literal

When you set a property on a primitive value, in fact you create a wrapper object around the primitive value and set the property on the wrapper object, not the primitive value.

a.foo = 30; // wrapper object created and directly thrown away
(new Number(a)).foo = 30; // --> equivalent

When you read the property, again you create a wrapper object, whihch hasn't the property defined.

a.foo; // wrapper object created, has no foo property
(new Number(a)).foo; // --> equivalent, evaluates to undefined
answered Mar 31, 2013 at 21:36

1 Comment

In Javascript, a function is an object, so you can set properties on it ... nice!
0

In javascript functions are objects. Or they inherit from objects, or something.

try doing

a = function(){};
alert(typeof a.prototype);
answered Sep 8, 2010 at 23:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.