Using Object.defineProperty to define property - Javascript Object

Javascript examples for Object:Object Property

Description

Using Object.defineProperty to define property

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {/*from w  ww  .  jav  a2  s  .c  o  m*/
function myClass(id) {
    this.em = document.getElementById(id);
    Object.defineProperty(this, 'html', {
        set: function(val) {
            console.log('Set to ' + val);
        }
    });
}
(new myClass('foo')).html = 'bar';
    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials