Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explanation of "Return Statement" #7 is misleading #92

Open
ryancwalsh opened this issue May 11, 2020 · 0 comments
Open

Explanation of "Return Statement" #7 is misleading #92

ryancwalsh opened this issue May 11, 2020 · 0 comments

Comments

@ryancwalsh
Copy link

Thank you so much for your list of helpful questions.

Looking at https://github.com/ganqqwerty/123-Essential-JavaScript-Interview-Questions#7-what-would-be-the-output-of-following-code--2, I think the explanation would make more sense like this:

(function () {
  console.clear();
  function getName1() {
    console.log("getName1", this.name);
  }
  Object.prototype.getName2 = function () {
    console.log("Object.prototype.getName2", this.name);
  };
  Object.prototype.getName3 = () => {
    console.log(
      "Object.prototype.getName3",
      this.name,
      "Why does this one not work?"
    ); //https://stackoverflow.com/a/31755263/470749 because "Arrow functions provide a lexical this. It uses the `this` that is available at the time the function is evaluated." See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
  };
  Object.prototype.getName4 = () => {
    console.log(
      "Object.prototype.getName4",
      Object.getPrototypeOf(this).name,
      "This one does not make sense"
    );
  };
  let personObj = {
    name: "Tony",
    print: getName1
  };
  personObj.print();
  personObj.getName2();
  personObj.getName3();
  personObj.getName4();
})();

Thanks again, and best of luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant