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

Support objects created with Object.create(null) #1017

Merged
merged 2 commits into from Oct 11, 2017

Conversation

asprouse
Copy link
Contributor

Summary

Proposed change:

Support objects created with Object.create(null) by following the no-prototype-builtins. This fix is important because it makes nunjucks compatible with this widely used ES5 feature (Object.create).

Closes #468

Checklist

I've completed the checklist below to ensure I didn't forget anything. This makes reviewing this PR as easy as possible for the maintainers. And it gets this change released as soon as possible.

Note no documentation update was necessary.

@ArmorDarks
Copy link

This isn't related to PR directly, but I still can't get why would someone create object from null?

@ArmorDarks
Copy link

Also, here #468 (comment) said that this method doesn't work that well in older browsers.

@asprouse
Copy link
Contributor Author

@ArmorDarks creating an object using Object.create(null) creates an object without a prototype which is suitable for using as a map. Instead of calling obj.hasOwnProperty(x) to check membership one can simply obj[x]. It seems that this is a technique used is several popular libraries (express and graphql off the top of my head).

As far as compatibility goes this PR only changes how hasOwnProperty is called obj.hasOwnProperty(x) -> Object.prototype.hasOwnProperty.call(obj, x). Both hasOwnProperty and call predate ES5 (ES3 and ES1 according to MDN) so even IE8 support shouldn't be a problem.

@fdintino
Copy link
Collaborator

Thanks for the PR! I agree that Object.prototype.hasOwnProperty.call(obj) is preferable to obj.hasOwnProperty().

@fdintino fdintino merged commit d77b2e3 into mozilla:master Oct 11, 2017
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.

obj.hasOwnProperty undefined is not a function nodejs
3 participants