Use "this" inside a function passed to __defineGetter__ - Javascript Object

Javascript examples for Object:Object Property

Description

Use "this" inside a function passed to __defineGetter__

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <p>Open the JavaScript console to see the output.</p> 
      <script>
Object.defineProperty(String.prototype, "foo", {
  get: function() {/*from ww w  .  java  2s  . c o  m*/
    // Here, `this` is the string
    return this.toUpperCase();
  }
});
console.log("hi there".foo);

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

Related Tutorials