nick said:
Thomas 'PointedEars' Lahn wrote:
[...]
// Get mutable object from primitive value, or a reference
// to an object member if memberName is provided.
Object.prototype.$ = function(memberName)
{
if (!memberName)
return Object(this);
v = function() { return this.$_object[this.$_member] };
return {$_object:this, $_member:memberName, valueOf:v, toString:v};
};
// Set the value of current object.
Object.prototype.$_ = function(value)
{
if (this.$_object)
this.$_object[this.$_member] = value;
else
this.valueOf = this.toString = function() { return value };
return this;
};
##
Alright, let me try to address some of your points.
[...]
- You want to avoid augmenting Object.prototype.
I'm not sure I do. How else will I be able to do things like this:
var foo = document.body.style.$('backgroundcolor');
Bad inference. The `style' property of those objects does _not_ refer to a
native object (but to a host object), so you cannot expect it to inherit
from Object.prototype or be extensible.
foo.$_('red'); // background of page turns red
No, it will not. Not least because the correct property name is
`backgroundColor'.
document.body.style.backgroundcolor = 'black';
// value of foo is now 'black'
Nobody wants to do that. Think of the performance penalty when calling a
prototype method -- and for what?
Where did I say objects had names? Are you referring to this:
"The '$' method will accept a single string parameter. If present, the
returned value will be a special reference to the named object member.
The result will appear to be a simple value, but it is linked with the
object member."
I meant here that the member of the object is named, not that the
object is named. The reference is to the member, not the object.
Do not let yourself be confused by the term `MemberExpression' in the
Specification: those are _not_ members, they are _properties_ (the term
"member" is only use for members of built-in *data types* in the
Specification). Properties can hold values. If they hold references to
objects (which are values), then that referred object has identity, not
name: it can be referred to by properties of different name of different
objects. However, you are false assuming that an object can only ever be
referred to by the same property, as your wrapper object has a property to
store only that one property's name.
Can you clarify? I don't understand this.
I think I was mistaken; `this' does not refer to an Activation Object here.
However, the `$_object' property (regardless of its name) does not make
sense except as a marker because its value is a reference to the calling
object (`this'): this.$_object[this.$_member] === this[this.$_member].
BTW, there's another potential problem: You try to the augment the instance
with a property `$_member', a property that becomes enumerable when set,
and error-prone to rely on when not set.
The supposed-to-be type-cast `Object(this)' is also pointless because in
the context in which it is used, `this' is already a reference to an
object; or, IOW, `this' *always* refers to an object (for primitive values:
due to the built-in wrapping object) and does not need to be type-casted
(provided that was possible so easily). Incidentally, when the Object()
function is called as a function or a constructor with an object reference
as argument, it simply returns that reference without creating a new object
(ES3/5, 15.2.1.1 and 15.2.2.1).
I'd expect it to with this code. Are you suggesting I force the result
to a primitive?
You better do, for which you would need to backup the original toString()
method.
How could it possible work transparently,
See my example. All that is additionally required then is defining a
setter (with Object.protoype.__defineSetter__ and the like; not fully
compatible, though); but then one could have used native setters and
getters to begin with.
as the language doesn't support any concept of pointers?
Non sequitur.
Using this would require rewriting all code, it just requires: (1) wrap
primitive in an object so you can use the $_() setter, and (2) use the
$_() setter to set a value when you want to modify the value "by
reference."
And it would require rewriting of all functions and methods that used the
wrapper object instead of the primitive value. Even though they only
required read access.
Where did I modify constants? Not sure what you mean here either.
`5' is a constant as is `"foo"'. Constant as in immutable. One would not
attempt to modify immutable values, or emulate that by keeping a wrapper
object to store another value. There is no advantage in doing that over
working with the primitive value, and there are several possible
disadvantages, some of which I have mentioned. So it remains a nice idea,
good for theoretical discussion what is possible, but not a practical one.
Here's a modified snippet....
[...]
...does that look any better?
Hardly.
Please learn to quote; trim your quotes to the relevant parts.
<
http://jibbering.com/faq/#posting> pp.
PointedEars