0

A declaration of a property with the name "name" is always an empty string, no matter how much I force (a hardcoded) a value into it. The weird part is, this only appears to be happening in Chrome and Firefox. IE works perfectly fine, and I didn't even intially built this for IE.

Here's a little snippet to reproduce the issue:

var fn = function() {
    this.name = "Hello World"
    this.foo = "bar"
}

// Create a blank function.
var obj = function() {};

// replacing the code below with 'var o = new fn()' doesn't make a difference.
var o = Object.create(fn.prototype);
fn.apply(o);

for (var i in o) {
    // This line will print:
    //     name = Hello World
    //     foo = bar
    console.log(i + ' = ' + o[i]);

    // Apply them to the 'empty' function.
    obj[i] = o[i];
}

console.log(obj.name);  // prints ""
console.log(obj.foo);   // prints "bar"

// Hardcode a value, just because I'm aggresive and frustrated.
obj.name = "test?"
console.log(obj.name); // still prints empty string :(

I'm not interested in hearing alternatives to the object construction, I just want to know why this particular implementation doesn't work in Chrome and FF while it does in all IE versions, including 11.

Pavlo
  • 43,301
  • 14
  • 77
  • 113
Harold
  • 1,372
  • 1
  • 14
  • 25
  • It's obviously because `obj` is a function, and a function already has a `name` property, it's built into the prototype, even if you didn't give the function a name, and you can't change a functions name later. – adeneo Jan 23 '14 at 21:42
  • ^^ Yup, just try `var obj = function lulz() {}` and you'll see "lulz". – elclanrs Jan 23 '14 at 21:43
  • 2
    possible duplicate of [Why can't I set a JavaScript function's name property?](http://stackoverflow.com/questions/18904399/why-cant-i-set-a-javascript-functions-name-property) – Bergi Jan 23 '14 at 21:43
  • And the reason it works with IE is because IE is strange. :) – useSticks Jan 23 '14 at 21:44

2 Answers2

2

obj is a function, and a function already has a name property, it's built into the prototype, even if you didn't give the function a name, and you can't change a functions name later, it's even in the specification

You cannot change the name of a function, this property is read-only:

MDN

If it works in IE, then IE is wrong.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • It's not my intention to change the entire name of a function. I merely want to have a property called "name" :( – Harold Jan 23 '14 at 21:45
  • 1
    @Harold - But the object is a function, and it already has a name property, it's the name you give the function or an empty string, and you can't change that. You have to either use another property or an object that is not a function. – adeneo Jan 23 '14 at 21:46
  • Alright, glad to know it wasn't a strange bug on my end then :) Thanks for the answers. (can mark as accepted in 3 minutes). – Harold Jan 23 '14 at 21:49
1

name is already a property of a function in some browsers,

or '' for anonymous functions.

var obj = function testfun() {};
obj.name

/*  returned value: (String) testfun */

And despite your uninterest in object creation syntax,

an object (but not a function) can have the name 'test':

var obj2={};
obj2.name='test';

obj2.name
/*  returned value: (String) test */
kennebec
  • 102,654
  • 32
  • 106
  • 127