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

fix(utils): Fixed getOwnPropertyDescriptor to hasOwnProperty function #908

Merged
merged 4 commits into from
Jan 30, 2024

Conversation

ssi02014
Copy link
Contributor

@ssi02014 ssi02014 commented Jan 29, 2024

Summary

@cure53 👋

The getOwnPropertyDescriptor static method returns an object describing the configuration of a specific property on a given object (that is, one directly present on an object and not in the object's prototype chain).

My understanding is that getOwnPropertyDescriptor within the cleanArray and clone functions is simply used to validate that the property exists. (Instead, the lookupGetter function actually uses the descriptor)

Therefore, I thought it would be more appropriate to utilize the hasOwnProperty function for the purpose of determining if a property exists. (Returns a boolean that suits our intent.)

const objectHasOwnProperty = unapply(Object.prototype.hasOwnProperty);

const arr = [1, 2];

console.log(objectHasOwnProperty(arr, 0)); // true
console.log(objectHasOwnProperty(arr, 1)); // true
console.log(objectHasOwnProperty(arr, 2)); // false

const obj = {
  foo: 1,
};

console.log(objectHasOwnProperty(obj, "foo")); // true
console.log(objectHasOwnProperty(obj, "bar")); // false

@@ -46,6 +46,8 @@ const stringReplace = unapply(String.prototype.replace);
const stringIndexOf = unapply(String.prototype.indexOf);
const stringTrim = unapply(String.prototype.trim);

const objectHasOwnProperty = unapply(Object.prototype.hasOwnProperty);
Copy link
Contributor Author

@ssi02014 ssi02014 Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaScript does not protect the property name hasOwnProperty, so objects with properties with this name may return incorrect results.

If we use the prototype function the way we've used it, it's stable.

const objectHasOwnProperty = unapply(Object.prototype.hasOwnProperty);

const obj = {
  foo: 1,
  hasOwnProperty() {
    return false;
  },
};

// Normal behavior
console.log(objectHasOwnProperty(obj, "foo")); // true
console.log(objectHasOwnProperty(obj, "bar")); // false

console.log(Object.prototype.hasOwnProperty.call(obj, "foo")); // true
console.log(Object.prototype.hasOwnProperty.call(obj, "bar")); // false

// Abnormal behavior
console.log(obj.hasOwnProperty("foo")); // false
console.log(obj.hasOwnProperty("bar")); // false

Related
MDN-Using hasOwnProperty as a property name


const objectHasOwnProperty = unapply(Object.prototype.hasOwnProperty);

const obj2 = Object.create(null);
obj2.foo = 1;

// Normal behavior
console.log(objectHasOwnProperty(obj2, "foo")); // true
console.log(objectHasOwnProperty(obj2, "bar")); // false

console.log(Object.prototype.hasOwnProperty.call(obj2, "foo")); // true
console.log(Object.prototype.hasOwnProperty.call(obj2, "bar")); // false

// TypeError: obj2.hasOwnProperty is not a function
console.log(obj2.hasOwnProperty("foo"));

Related
MDN-Objects created with Object.create(null)
eslint no-prototype-builtins

@cure53
Copy link
Owner

cure53 commented Jan 30, 2024

This looks good, thanks a lot! 😄

@cure53 cure53 merged commit cb18519 into cure53:main Jan 30, 2024
8 checks passed
@ssi02014 ssi02014 deleted the fix/utils branch January 30, 2024 07:01
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.

2 participants