Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Maker function instead of Function constructor #173

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

Merged
merged 2 commits into from
Jan 25, 2017

Conversation

DavidVujic
Copy link
Contributor

Function constructors should be avoided. Using the this keyword will increase the risk for bugs. An example: if not using the new keyword when creating an instance of the Employee object, this will be bound to the global object, and not to the Employee instance.

Use a maker function instead, returning an object, using variables existing in the scope of the function (closure).

@DavidVujic DavidVujic changed the title Object instead of Class Object instead of Function constructor Jan 24, 2017
@DavidVujic DavidVujic changed the title Object instead of Function constructor Maker function instead of Function constructor Jan 24, 2017
@vsemozhetbyt
Copy link
Contributor

Maybe even simpler:

function makeEmployee(name) {
  return {
    getName() {
      return name;
    },
  };
}

Also beware 4 spaces)

@ryanmcdermott ryanmcdermott merged commit 348760e into ryanmcdermott:master Jan 25, 2017
@ryanmcdermott
Copy link
Owner

Awesome David thank you!

@DavidVujic DavidVujic deleted the suggestions branch January 28, 2017 12:41
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

Successfully merging this pull request may close these issues.

3 participants