domingo, 21 de noviembre de 2010

En esta historia Frodo es ECMAScript

Para expresar mi opinión sobre la introducción de los getters/setters en ECMAScript se me ocurrió este "cuento" que dejé en el blog de Angus. De paso probamos a ver si Blogger le pone pegas a meter un style en los tags, concretamente con uno de mis degradados de color.
Object.prototype.takeGiftFromSauron = function() {
  function makeRingWraith() {
    var me = this;
    Object.getOwnPropertyNames(me).forEach(function(name){
      if (name === 'Object') return;
      var bag = Object.getOwnPropertyDescriptor(me,name);
      if (bag.configurable) delete me[name];
      else if (bag.writable) me[name] = null;
      else alert('damn '+name+'!');
    });
  }
  Object.defineProperty(this,'ring',{get:makeRingWraith});
  return this;
}

var Frodo = Object.defineProperties({},{
  'coins':{value:3,enumerable:true},
  'soul': {value:'simple',enumerable:true,configurable:true},
  'sword':{value:'elfic',enumerable:true,configurable:true},
  'cape': {value:'brown',enumerable:true,writable:true}
});

// Frodo do you like this innocent ring?
Frodo.takeGiftFromSauron();
// Wear the ring Frodo!!
alert(Frodo.ring);
// Surprise! Little avaricious hobbit, where are your soul and your elfic sword?

It works! Blogger parece aceptar cualquier cosa. Empieza gustarme :D

No hay comentarios:

Publicar un comentario