api mapping

# memolus at googlemail.com (16 years ago)

Hello. I want to map document.getElementById("title").value to gui.title in my ecmascript application. So I can use several implementations of gui (html, gtk+, xul, qt, etc.) But there's no way to reference to this "title", because it's a primitive data type, a "String". I've tried the following: let title = document.getElementById("title"); gui.defineGetter("title", function () {return title.value;}); gui.defineSetter("title", function (x) {title.value = x;return x;}); But this code looks ugly and isn't abstract programming. So created the following helper function: function link(obj, prop, target, tprop) { obj.defineGetter(prop, function () {return target[tprop];}); obj.defineSetter(prop, function (x) {target[tprop] = x;return x;}); } It can be used this way: let title = document.getElementById("title"); link(gui, "title", title, "value"); I think it would be easier if such a feature would be built-in in ecmascript. If you know a better already-possible way, please let me know.

# David-Sarah Hopwood (16 years ago)

memolus at googlemail.com wrote:

Hello. I want to map document.getElementById("title").value to gui.title in my ecmascript application. So I can use several implementations of gui (html, gtk+, xul, qt, etc.) But there's no way to reference to this "title", because it's a primitive data type, a "String". I've tried the following: let title = document.getElementById("title"); gui.defineGetter("title", function () {return title.value;}); gui.defineSetter("title", function (x) {title.value = x;return x;}); But this code looks ugly and isn't abstract programming. So created the following helper function: function link(obj, prop, target, tprop) { obj.defineGetter(prop, function () {return target[tprop];}); obj.defineSetter(prop, function (x) {target[tprop] = x;return x;}); } It can be used this way: let title = document.getElementById("title"); link(gui, "title", title, "value"); I think it would be easier if such a feature would be built-in in ecmascript. If you know a better already-possible way, please let me know.

esdiscuss/2009-February/008875

(You've asked variations on this question three or four times now. I don't mind repeating myself a few times, but not indefinitely. The feature is already in ES5 for properties, it will not be added for local variables, 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.)

# memolus at googlemail.com (16 years ago)

Just remind that link() isn't build-in. Any developer would have to set this function on his own. Also it's not so sweet like gui.title := document.getElementById("title").value which would set an real reference to an primitive data type, or: gui {title :: document.getElementById("title").value } Just ideas to make programming easier.

# memolus at googlemail.com (16 years ago)

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
		});
}

}

# Mike Samuel (16 years ago)

2009/12/27 memolus at googlemail.com <memolus at googlemail.com>:

Just remind that link() isn't build-in. Any developer would have to set this function on his own.

Or use a library that defines it.

# David-Sarah Hopwood (16 years ago)

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.)