How comes properties act like functions?

If you install colors you will see you can write scripts like this:

var colors = require('colors');

console.log('i like cake and pies'
console.log('inverse the color'.inverse);
console.log('OMG Rainbows!'.rainbow);
console.log('Run the trap'.trap);

How is is possible that properties behave like functions (like [5, 6, 4].count?).

  • Copying Javascript getters/setters to another prototype object
  • speed of getter function vs direct access
  • “Get” not working
  • Cross-browser Getter and Setter
  • Javascript setter for value of object
  • Set undefined javascript property before read
  • I understand 'Run the trap'.trap() but not 'Run the trap'.trap

  • Unexpected behaviour in Firefox when getter returns a function
  • Copying Javascript getters/setters to another prototype object
  • Javascript defineGetter
  • Javascript getters/setters in IE?
  • Cross-browser Getter and Setter
  • “Get” not working
  • 2 Solutions collect form web for “How comes properties act like functions?”

    JavaScript allows you to define getters for setters and properties (even on prototypes):

    Object.defineProperty(Array.prototype, 'count', {
        get: function () {
            return this.length;
    console.log([1, 2, 3].count);

    Use sparingly. colors, specifically, uses the non-standard __defineGetter__ function instead, but to the same effect.

    This can be achieved using getters:

    Object.defineProperty(String.prototype, "testing", {
        get: function() {
            return this.string + ' some test message ';
    console.log( 'my string'.testing ); // my string some test message