0
const person = {
    name: "Mike",
    country: "New Zealand"
}

function personUpdate(name, country) {
    this.name = name
    this.country = country
}

personUpdate.bind(person)

personUpdate('Tony', 'Chile')

Why doesn't this work? person still has original properties 'Mike' and 'New Zealand'. Why doesn't personUpdate.bind(person) I want to make it so that every call to personUpdate the this refers to the person object (and without using new).

1
  • 1
    var boundPersonUpdate = personUpdate.bind(person); boundPersonUpdate('Tony', 'Chile'); Commented Jul 4, 2017 at 1:48

2 Answers 2

2

Calling .bind doesn't modify the function you pass in; it returns a new bound function.

So you want either:

var boundPersonUpdate = personUpdate.bind(person);
boundPersonUpdate(...); // call the bound version

or:

personUpdate = personUpdate.bind(person); // overwrite the original function
Sign up to request clarification or add additional context in comments.

Comments

0

I have tried your code and I think nothing is wrong with it.

const person = {
name: "Mike",
country: "New Zealand"
}
function personUpdate(name, country) {
this.name = name;
this.country = country;

console.log(this.name);
console.log(this.country);
}
personUpdate.bind(person);
personUpdate('Tony', 'Chile');

I tried to print it and it is giving me "Tony" and "Chile", or I misunderstood your question?

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.