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;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You really can’t do this because it’s a syntax error
I suppose you simply want to say:
Anyway. The reason that you cannot get a property out of a number, is that it is not a real Object.
Believe it or not, the same goes for strings:
However if it’s String object, then it works as expected:
Or if it’s an Number object. You get the point.
String and number literals are not objects in Javascript. That’s the reason.