Akg
Akg

Reputation: 359

javascript object name property

This could be a very basic javascript concept, but I don't find the right answer, so asking the question here.

var obj = {};
var fn = function() {};
obj.name = 'something';
fn.name = 'something';

if (obj.name == fn.name) { console.log('both objects have same property'); }

In the above code, if block doesn't execute. If I use something else like .prop, instead .name, it works.

obj.prop = 'something';
fn.prop = 'something';

For the above values, if block executes.

Why is the if block executes for the first case, and not for the second one.Is that .name a reserved keyword/property in js? or something else I am missing here?

Upvotes: 0

Views: 43

Answers (2)

Quentin
Quentin

Reputation: 944528

From MDN:

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

From ECMA-262 6th Edition / Draft April 27, 2014:

This property has the attributes { [[Writable]]: false,

Upvotes: 5

Sandro Paganotti
Sandro Paganotti

Reputation: 2313

You wrote obj.name = 'soemthing'; instead of obj.name = 'something'; :)

Upvotes: 0

Related Questions