TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript handler defineProperty() Method

JavaScript handler defineProperty() Method with examples on javascript handler, javascript methods, javascript tutorial, apply(), construct(), defineProperty(), deleteProperty(), get(), setPrototypeOf(), set(), ownKeys(), isExtensible() etc.

<< Back to JAVASCRIPT

JavaScript handler.defineProperty() Method

The handler.defineProperty() method can define the new properties. It can also modify the existing properties directly on an object. It is trap for Object.defineProper(). This method can be used in two situations.

  1. When we wanted to ensure cross-browser support of getters and setters.
  2. Whenever we want to define a custom property accessor.

Syntax

defineProperty: function(target, property, descriptor)

Parameters

Target: The target object.

Property: Retrieved property description.

Descriptor: The property being defined or modified.

Return value

This method returns a Boolean

Browser Support

Chrome 49
Edge 12
Firefox 18
Opera 36

Example 1

var target = {}
target.foo = 'bar'
document.writeln(Object.getOwnPropertyDescriptor(target, 'foo'))
Test it Now

Output:

[object Object]

Example 2

var xyz = {};
var proxy = new Proxy(xyz, {
  defineProperty: function(target, name, propertyDescriptor) {
document.writeln('in defineProperty');
return Object.defineProperty(target, name, propertyDescriptor);
  }
});
Object.defineProperty(proxy, 'bar', {} );
Test it Now

Output:

in defineProperty

Example 3

var xyz ={};
var hu = {};
var proxy = new Proxy(xyz, {
   defineProperty: function(target, name, propertyDescriptor) {
    document.writeln('Learn Java Script');

       return Object.defineProperty(target, name, propertyDescriptor);
  }
      });
var pro= new Proxy(hu, {
  defineProperty: function(target, name, propertyDescriptor) {
    document.writeln('Proxt Method ');
       return Object.defineProperty(target, name, propertyDescriptor);
}
      });
   Object.defineProperty(proxy, 'bar', {} );
   Object.defineProperty(pro, 'hug', {} );
Test it Now

Output:

Learn Java Script Proxt Method

Next TopicJavaScript handler




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf