Here's the code I'm working with at the moment:
Object.defineProperty(String.prototype, "testy", {
get: function() {
return this.string;
},
set: function(string) {
this.string = string;
}
});
console.log("tessfef3t".testy());
Before I would've used String.prototype.testy = function {}
, however I've been told that using something similar to the the code above is the better way to do it. I'm not sure how that is meant to work but I haven't got that code to work yet.
Could someone show me how to correctly do what I'm doing?
Thanks
testy
is kind of a "fake" property -- it has no value of its own, but setting or getting its value will invoke its set
and get
functions. You can use it like:
var foo = new String();
// this invokes the setter and sets foo.string to 5:
foo.testy = 5;
// this invokes the getter and prints the value of foo.string
console.log(foo.testy);
// this prints 5, because foo.string has been set by the setter
console.log(foo.string);
EDIT:
Now I see your comment about what you actually want to happen. It's not possible to alter the value of a string object, because strings are immutable. You'd have to destroy the string object and replace it with a new one, which is not possible within the object's own member function.
If all you're doing in your getter and setter is setting a property that is not used elsewhere and there's no other processing then you gain nothing by using a getter and setter so you're better off with your original approach. Furthermore, you won't be able to retrieve a property you set on a string anyway. A string value in JavaScript (which is what a string literal such as "foo"
creates, and is the only kind of string that is generally useful) is not an object so attempting to accessing a property requires special handling: effectively a temporary String
object is created and thrown away immediately afterwards.
Finally, another major disadvantage of getters and setters is that they are specified in ECMAScript 5 and as such are only available in relatively recent browsers. They aren't implemented in IE < 9, for example.
This worked fine for me:
Object.defineProperty(String.prototype, "byteLength", {
get: function() {
var str = "" + this; // Get internal value
// Compute size in actual bytes vs Unicode characters
// per http://stackoverflow.com/a/23329386/912236
for (var b = str.length, c = str.length - 1; 0 <= c; c--) {
var a = str.charCodeAt(c);
127 < a && 2047 >= a ? b++ : 2047 < a && 65535 >= a && (b += 2);
56320 <= a && 57343 >= a && c--;
}
return b;
}
});
> "Hello".byteLength
< 5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.