0

I wasn't sure how to ask this, and I apologize if it's been asked already, but I could not find it or an answer.

What is returned when you assign a function to a var.

    var obj = function(){
        this.name = 'Steve';
    }
    alert( obj.name === '');//true
    obj.name = 'Mike';
    alert( obj.name === '');//true
    obj.foo = 'fall';
    alert( obj.foo );//fall

I know obj is now a function/object and I can call obj(). I also know that the function attached to obj is a constructor for new objects of type obj. If I do

var newObj = new obj();
alert( newObj.name );//Steve

"Steve" is printed. etc. etc.

Why does obj seem to have a property called "name" attached to it? And why when I try to assign a value to that property does it not get assigned?

0

3 Answers 3

5

Because function objects have aname property (which is non-standard by the way) that represents the "name" of the function, your function shows an empty string ("") as the value of this property because it's anonymous.

For example, if you declare a function with the name foo:

function foo() {}
foo.name; // "foo"


var anon = function () {};
anon.name; // "", an anonymous function

var namedFunctionExpr = function bar () {};
namedFunctionExpr.name; // "bar"

Also as you noted, you can't change the value of this property, since in most implementations, it's non-configurable, non-enumerable and non-writable:

Object.getOwnPropertyDescriptor(function x(){}, 'name'); 
/* logs:
{ 
  configurable: false,
  enumerable: false,
  value: "x",
  writable: false
}
*/

Remember, this property is non-standard and there are some implementations that don't support it (e.g. all IE JScript versions do not support it)

Now as @Felix suggest, I would like to add that this.name doesn't have anything to do with your function's name property.

In your example, calling the obj function as a constructor (with the new operator), sets the this value to a newly created object, that inherits from your function's prototype, and you are assigning a value to the name property to this new object.

Sign up to request clarification or add additional context in comments.

1 Comment

It's probably worth pointing out explicitly that obj.name has nothing at all to do with this.name.
1

Without constructing an instance of an object with the function, using obj.name, you're just reading the name property of the function.

You can actually explicitly name a function with the syntax

function foo(){

}

you could do something like:

var goo = function foo() {}

and goo.name will actually read "foo".

Your declaration here is not named:

var obj = function(){
        this.name = 'Steve';
    }

so obj.name is always the empty string.

You can read up more here:

http://bonsaiden.github.com/JavaScript-Garden/#function

However, if you invoke the function with new, you get an instance of an object created through the constructor, complete with everything inherited through the prototype chain.

It's worth noting however, that you can actually return any object you want from a constructor function. By default it returns an instance of the object, but you could just as easily return a different object, which naturally wouldn't have any of the properties from the prototype.

ie:

function Dog() {
   this.bark = "bark!";
   return { bark: "quack"};
}

var weirdDog = new Dog();

will not have weirdDog.bark === "bark".

Again, you can read up more on the Javascript Garden site for more info.

2 Comments

Constructor functions always have to return an object. If you return a primitive, this will be returned instead.
Ah yes, this is true. Forgot about that. Updating the answer to reflect.
0

"name" property returns the name of the function and you can't set it because it's readonly property. It's returning blank because your function is anonymous.

Better check this link for more details:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Name

Comments

Your Answer

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.