JavaScript defineProperty() method

The Javascript handler defineProperty() method defines the new properties and often modifies the existing properties.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
defineProperty: function(target, property, descriptor)
defineProperty: function(target, property, descriptor)
defineProperty: function(target, property, descriptor)

Parameters:
target: It represents the target object.
property: It represents the property description.
descriptor: It represents the property being defined or modified.

Return:
Boolean.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
var handler = {};
var define = new Proxy(handler, {
defineProperty: function(a,b,c) {
document.writeln('HELLO WORLD!');
return Object.defineProperty(a,b,c);
}
});
Object.defineProperty(define, 'bar', {} );
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> var handler = {}; var define = new Proxy(handler, { defineProperty: function(a,b,c) { document.writeln('HELLO WORLD!'); return Object.defineProperty(a,b,c); } }); Object.defineProperty(define, 'bar', {} ); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script>
var handler = {};
var define = new Proxy(handler, {
defineProperty: function(a,b,c) {
document.writeln('HELLO WORLD!');
return Object.defineProperty(a,b,c);
}
});
Object.defineProperty(define, 'bar', {} );
</script>
</body>
</html>