api mapping

David-Sarah Hopwood david-sarah at jacaranda.org
Sun Dec 27 09:51:55 PST 2009


memolus at googlemail.com wrote:
> David-Sarah Hopwood wrote at 25th December:
>> and there is no need for a 'link' convenience function to be standardized
>> given that it is a 5-liner in terms of Object.defineProperty
> 
> Just have a look at the following programming code with *sweet* 5-liners:
> 
> var Gui = function()
> {
> 	this.init.apply(this, arguments);
> }
> 
> Gui.prototype = new function()
> {
> 	this.init = function()
> 	{
> 		let title = document.getElementById("title");
> 		Object.defineProperty(this, "title",
> 			{get: function() { return title.value; },
> 			set: function(x) { title.value = x; },
> 			enumerable: true
> 		 	});
> 
> 		let url = document.getElementById("url");
> 		Object.defineProperty(this, "url",
> 			{get: function() { return url.value; },
> 			set: function(x) { url.value = x; },
> 			enumerable: true
> 			});
> 
> 		let input = document.getElementById("input");
> 		Object.defineProperty(this, "url",
> 			{get: function() { return input.value; },
> 			set: function(x) { input.value = x; },
> 			enumerable: true
> 			});
> 	}
> }

Here's how I would do it in ES5:

function makeGui(doc) {
  /*const*/ var title = doc.getElementById("title"),
                url = doc.getElementById("url"),
                input = doc.getElementById("input");

  return Object.freeze({
    get title()         { return title.value; }
    set title(newValue) { title.value = newValue; }
    get url()           { return url.value; }
    set url(newValue)   { url.value = newValue; }
    get input()         { return input.value; }
    set input(newValue) { input.value = newValue; }
  });
}

(I'd probably do more validation, but that would be a less fair comparison.)

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 292 bytes
Desc: OpenPGP digital signature
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20091227/4338ed67/attachment.bin>


More information about the es-discuss mailing list