onchange event
You can use defineGetter and defineSetter on window in current browsers (excluding IE). In ES3.1 you can use Object.defineProperty instead.
On Sun, Mar 8, 2009 at 12:44 PM, Erik Arvidsson <erik.arvidsson at gmail.com> wrote:
You can use defineGetter and defineSetter on window in current browsers (excluding IE). In ES3.1 you can use Object.defineProperty instead.
Memolus does not say what he needs this for, but it sounds even more ludicrous than the recent discussion to allow [[Writable]] name to anonymous function objects created in a certain way because the "Objective-J" folks wanted it (what was that use case?)
Garrett
On Sun, Mar 8, 2009 at 05:39, memolus at googlemail.com <memolus at googlemail.com> wrote:
I like to use an onchange event.
Example:
// Create a global property named "money" and assign the value 100. // Note that this will fail in all versions of IE if there is an element with an // id="money" (which appears to be true).
money = 100;
// Assign an "onchange" property to a temp wrapped object, giving a // function value that is immediately eligible for garbage collection.
money.onchange = function() { refreshMoneyDisplay(); if(this <= 0) { alert("You do no have money anymore.") } };
// Create a global method "refreshMoneyDisplay".
refreshMoneyDisplay = function() {
// call host method to get an element, assiging the value // money to its value property.
Garrett, I gave a specific use case for why I'd like to be able to
specify the name on an anonymous function: wrapping an existing
function with pre- or post-call behaviour.
I don't think the desire to trigger a function on change of a value is
at all ludicrous. I think we should cut Memolus some slack for not
being at his most articulate.
On Mon, Mar 9, 2009 at 8:35 AM, Jeff Watkins <watkins at apple.com> wrote:
Garrett, I gave a specific use case for why I'd like to be able to specify the name on an anonymous function: wrapping an existing function with pre- or post-call behaviour.
I think I get it.
It seems like just giving the function a name (not anonymous) would fulfill that need.
Example:-
<script>
var getX = (function(){ var x = 10; function getX() { return x; } return getX; })();
document.write(getX.name + ": " + getX()); </script>
I don't think the desire to trigger a function on change of a value is at all ludicrous.
What would the type of the variable be? Number? A number with a behavior? How would you modify ToNumber or ToObject to accomodate for those changes?
If an object is wanted, an object should be used. Objects encapsulate behavior; primitives do not.
I think we should cut Memolus some slack for not being at his
most articulate.
Sorry, I think the code says enough.
Garrett
On Mar 9, 2009, at 2:06 PM, Garrett Smith wrote:
On Mon, Mar 9, 2009 at 8:35 AM, Jeff Watkins <watkins at apple.com>
wrote:Garrett, I gave a specific use case for why I'd like to be able to
specify the name on an anonymous function: wrapping an existing function
with pre- or post-call behaviour.I think I get it.
It seems like just giving the function a name (not anonymous) would fulfill that need.
Example:-
<script> var getX = (function(){ var x = 10; function getX() { return x; } return getX; })();
document.write(getX.name + ": " + getX()); </script>
It is sometimes unfortunate that Function's Identifier can not be set
from a string value, without resorting to eval
. Descriptive
Identifier's make debugging easier. A simple example of some kind of
constructor creation facility shows what I mean:
var Class = { create: function(name) { var fn = eval('(function ' + name + '(){})'); // set up inheritance, etc. return fn; } }
Class.create('Person'); // Person()
I think it could be beneficial if Function.create
would allow for
such control.
[...]
On Mar 9, 2009, at 2:40 PM, Juriy Zaytsev wrote:
It is sometimes unfortunate that Function's Identifier can not be
set from a string value, without resorting toeval
. Descriptive
Identifier's make debugging easier. ...
I think this is actually an important use case - debugging. There is
a similar problem with folks who build frameworks that eval() code -
it's difficult, in debuggers like FireBug and Web Inspector, to
disambiguate these from the places such things show up in lists, since
the artifacts tend to have the same name like "Program" "eval'd
source" or whatever.
FireBug added some new behavior to display the first few bytes of the
eval()'d source, to help disambiguate. I've now abused this so that I
put a comment in front of eval()'d code that has an identifying mark.
Hopefully, the following picture shows what I'm doing:
http://www.flickr.com/photos/pmuellr/3256189331/
A nicer approach would be to define some kind of JavaDoc-like comment
tag that would be used for the name. This could be used to support
both eval() and Function().
Another approach, for Function, might be for debuggers to support a
notion of "debugToString()", which would get invoked in lieu of
toString() for the debugger's own purposes.
In all the approaches, nothing actually needs to get defined in terms
of the language, just in terms of additional things debuggers could
support, that developers could take advantage of. If one of the
primary use cases for naming is debug, this is probably a better
approach to go down - extend the debuggers - rather than deal with it
at a language level.
Patrick Mueller - muellerware.org
I like to use an onchange event.
Example:
money = 100; money.onchange = function() { refreshMoneyDisplay(); if(this <= 0) { alert("You do no have money anymore.") } }; refreshMoneyDisplay = function() { document.getElementById("money").value = money; }