Why Object.getOwnPropertyDescriptor ?
Object.getOwnPropertyDescriptor () returns a property identifier for the specified property of the specified object. This method allows examination of the precise definition of a property. A property in JavaScript consists of a name in the string value or a Symbol and property identifier. More information about property identifier types and attributes can be found in Object.defineProperty ().
Note that this is not a method to call on an object: it is a public function and you must pass it on to an object.
Syntax:
Object.getOwnPropertyDescriptor( obj , name )
- obj: The object that is to have its property attributes queried.
- name: The name of the property (or index of the array element) to query.
A property descriptor is an ordinary JavaScript object that describes the attributes of a property. There are two kinds of JavaScript properties. A data property has a value and three attributes: enumerable, writable, and configurable. An accessor property has a getter and/or a setter method as well as enumerable and configurable attributes.
- value: The value for the property.
- writable: if true and only if the associated value can be changed.
- receive: A function that functions as a recipient for the property or that is undefined if a recipient does not.
- set: By default, undefined. It acts as a tuner for the property.
- enumerable: If true and only if this property occurs during enumeration of properties on the corresponding object.
JavaScript Object.getOwnPropertyDescriptor() Examples
Example 1
let obj = {
website: "truecodes.org"
};
let arr = Object.getOwnPropertyDescriptor(obj,"website")
console.log(arr.configurable);
console.log(arr.value)
output:
true
truecodes.org
Example 2
let obj = {
number: 10
}
let arr = Object.getOwnPropertyDescriptor(obj, 'number');
console.log(arr.configurable);
console.log(arr.enumerable);
console.log(arr.value);
output:
true
true
10
Example 3
let obj = {
foo: "true",
get name() { return "codes" },
};
console.log(Object.getOwnPropertyDescriptors(obj))
output:
configurable: true
enumerable: true
value: “true”
writable: true
Example 4
let myObject, arr;
myObject = { get foo() { return 17; } };
arr = Object.getOwnPropertyDescriptor(myObject, 'foo');
console.log(arr)
output:
configurable: true
enumerable: true
get: ƒ foo()
set: undefined
Example 5
For this example, you need to know the “object” topics:
- Object.getOwnPropertyDescriptor()
- Object.prototype.hasOwnProperty()
- Object.values
<input type="text" id="username" />
<input type="text" id="password" />
<button onclick="login();">LOGİN</button>
<script>
function login() {
var users = {
"alex": 123,
"rick": 234,
};
let username = document.getElementById("username").value,
password = document.getElementById("password").value;
if (users.hasOwnProperty(username) == true) {
let arr1 = Object.getOwnPropertyDescriptor(users, "alex");
let arr2 = Object.getOwnPropertyDescriptor(users, "rick");
if (arr1.value == password) {
alert("welcome " + username);
} else if (arr2.value == password) {
alert("welcome " + username);
}else {
alert("You are entering the password incorrectly.")
}
} else {
alet("You are entering the username incorrectly.")
}
}
</script>
Resources:
Browser Support
| Chrome | 5 |
| Edge | yes |
| Firefox | 4 |
| Internet Explorer | 9 |
| Opera | 11.6 |
| Safari | 5 |
| Android webview | yes |
| Chrome for Android | yes |
| Edge mobile | yes |
| Firefox for Android | 4 |
| Opera Android | 11.5 |