askoasksao
askoasksao

Reputation: 97

Javascript printing things that are not inside the code

I have this code in my JS

var customer = {
    name: "John Jack",
    speak: function(){
        return "my name is "+name;
    },
    address:{
        street: '123 main st',
        city: 'Pittsburgh',
        state: 'PA'
    }
}

document.write(customer.speak());

In my HTML i expected

my name is John Jack

But instead i got something really weird

my name is Peaks mirroring in a lake below, Stubai Alps, Austria

I have some theories that this is somehow connected to the Chrome extension i'm using called "Pixlr",but i don't see how my js code could connect to that.I tried changing variable name and speak to say,but it still prints the same thing.What's wrong?

Upvotes: 0

Views: 39

Answers (1)

brk
brk

Reputation: 50326

replace name with this.name

var customer = {
  name: "John Jack",
  speak: function() {
    return "my name is " + this.name;
  },
  address: {
    street: '123 main st',
    city: 'Pittsburgh',
    state: 'PA'
  }
}

document.write(customer.speak());

Upvotes: 2

Related Questions