VD27
VD27

Reputation: 31

JS object behavior is different when I change the property value

I'm completely new to JS and trying to learn on my own. Using below code -

var me = {
    name: {first:"justin"}
},
name = me.name;

name = {first: "alexis"};

Why would document.write(me.name.first + "</br>"); return justin?

and

why would document.write(this.name.first); doesn't return anything?

Please can you explain me?

Thanks, Me

Upvotes: 0

Views: 69

Answers (1)

JasmineOT
JasmineOT

Reputation: 2068

Just change the variable name name to other string, for example: n. Everything will work perfect.

var me = {
   name: {first:"justin"}
},
n = me.name;

n = {first: "alexis"};

The reason is this.name.first will refer to window.name.first. But window.name has special usage in javascript and has to be a string.

Upvotes: 1

Related Questions