Skip to content

Instantly share code, notes, and snippets.

@Swivelgames
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swivelgames/ca277d2bbf0c23cbef42 to your computer and use it in GitHub Desktop.
Save Swivelgames/ca277d2bbf0c23cbef42 to your computer and use it in GitHub Desktop.
Messing around with things and trying to find a good way to make a string reference-able...
var StringReference = function(scope, str) {
if(str===void 0) { str = scope; scope = window; };
Object.defineProperty(scope, "__"+str, {
configurable: true,
enumerable: false,
get: function(){
return scope[str];
},
set: function(val){
return scope[str] = val;
}
});
var ret = new Function();
ret.prototype.valueOf = ret.prototype.toString = function(){
return this.get();
};
ret.prototype.set = function(val) {
return scope["__"+str] = val;
};
ret.prototype.get = function() {
return scope["__"+str]
};
return new ret();
};
@Swivelgames
Copy link
Author

Example:

var foo = "bar";
var ref = new StringReference("foo");

ref.set("rawr");
console.log(foo); // rawr

Unfortunately there doesn't seem to be a way to get a property, as in a portable reference to one bound getter and one bound setter...

In other words, I can't define a setter and getter for property "x", and then reference the bound setter/getter combination elsewhere.

Example:

var x = {
    get foo(){ return this._foo; },
    set foo(var){ return this._foo = var; }
};

I cannot take foo, bind it to x.foo, and then transport it elsewhere and still utilize the getter and setter.

Hypothetical code:

var fooProp = Object.getPropertyInstance(x, "foo");
fooProp = "hello world";
console.log(x.foo); // "hello world"

Unfortunately, getPropertyInstance doesn't actually work because the setter and getter functionality is so native that JavaScript itself doesn't entirely understand it. It's a function of the engine entirely and the only way to really figure out if a setter or getter exists on a property is to run __lookup(S|G)etter__ on the parent object. Unfortunately, we cannot retrieve a reference to the mechanism behind the mysterious foo property after we define getters and setters for it.

@Swivelgames
Copy link
Author

The real issue here is that there is no such thing as a native Property prototype, so there is no real "instance" of anything to retrieve.

Though, a Property prototype that does not have a value, and is instead bound to a certain scope item, might look something like this if it were to be implemented:

Property.prototype = {
    __getter__: function(){ ... },
    __setter__: function(val){ ... },
    __instance__: self, // this wouldn't be a reference to the value, but the actual instance of 'Property'
    __scope__: [object Object] // this would be a reference to the "parent" object that it was initially defined on, maybe?
}

In this case, if you had an instance foo that lived in x.foo, you could store the instance of x.foo into another variable by using the following...

var fooProp = x.foo.__instance__;
fooProp = "hello world";

This would then, going by the original example in the previous comment, eventually set x._foo to "hello world"; So, retrieving fooProp, x.foo, or x._foo would return "hello world".

With this, it could open the door for any type of variable to be reference-able.

Let me give you an example and I'll be done...

Say you want to pass a String (which is immutable) to a function and change it by reference. By allowing for a Property instance, this would now be possible:

Example:

var MyObj = {
    get prop(){ return this._prop; },
    set prop(val){ return this._prop = val; },
};

function setString(str, val) {
    return str = val;
}

var myString = MyObj.prop.__instance__;

setString(myString.__instance__, "newValue");

console.log(myString);
console.log(MyObj.prop);
console.log(MyObj._prop);

All of the three above console.log statements would return an immutable string of "newValue".

@Swivelgames
Copy link
Author

The issue I noticed when writing the above example was the fragility involved in passing around the portable property. Because of the fragility, accidentally passing in myString instead of myString.__instance__ would no longer allow the setter to be called within the setString() method.

Because of this, the only other option would be to make it impossible to dereference and just scrap the __instance__ method.

The issue with this is that this sort of functionality could be incredibly cumbersome for JavaScript engines to implement. This type of functionality would probably have a couple implementations at most before dying off, with browsers like Internet Explorer shrieking at the idea that they might have to implement something as difficult as setters/getters in the first place.

@Swivelgames
Copy link
Author

The nice thing about the "old fashioned," manually written getter/setter objects was that they were portable regardless of their value.

Using myVar.getValue() and myVar.setValue() would always update the variable in the initial scope back when we declared the myVar object, and we could lug it around wherever we needed to. Unfortunately, we cannot do this with native setters/getters...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment