1

I am trying to set up a getter on a native String in JavaScript and I can't seems to get it to work. Is this even possible?

var message = "foo";
message.__defineGetter__("length", function() {
  return 3;
});

(This is for work on the Terminal)

Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134
  • 1
    As far as I'm aware, the `length` property of a string object is read-only. – Qantas 94 Heavy Jun 08 '13 at 03:17
  • Why not just make your own method? Seems alot of hassle for little gain. – adrian Jun 08 '13 at 03:19
  • This is probably not a duplicate, but you might find it interesting/useful: http://stackoverflow.com/questions/6050289/setter-getter-not-working-for-number-literal – markasoftware Jun 08 '13 at 03:20
  • That's because I send a String with ANSI character in a Node.js module, and I want it to read correct actual seen character length. So I guess it need to be a string, I could try to send a custom object with a `toString`... – Simon Boudrias Jun 08 '13 at 03:21
  • _"Once a String object is created, this property is unchanging. It has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }."_ -- http://es5.github.io/#x15.5.5.1 – kangax Jun 08 '13 at 09:13

1 Answers1

2

Your question is a bit odd - why would you need to set getters when the properties you are interested in are already accessible?

That said: some of them are, some of them are not, because some of them have to stay what the ECMAscript spec says they should be for JavaScript to work properly. String's length property is an example of an immutable property, but most toString() properties are perfectly mutable (with hilariously detrimental results).

So the real question is "what are you trying to do that requires you to overrule the ECMAscript spec" =)

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • That's because I send a String with ANSI character in a Node.js module, and I want it to read correct actual seen character length. – Simon Boudrias Jun 08 '13 at 17:31
  • forgot to respond to this comment: if you use XHR to talk to node.js, by W3C decree your data must be in UTF-8 encoding. Browsers are not allowed to understand anything else for ajax requests, so if you're sending data in ANSI encoding (actually 8859-1), you'll have to turn that into UTF-8 first, then send it out as response on the node.js side. – Mike 'Pomax' Kamermans Jan 30 '14 at 16:37
  • Hey Mike, I wasn't working on browser stuff, but on Terminal issues. In the end, Node.js 0.11 fixed the ANSI encoding length issues, and I back fixed it by simply adding a façade to the Readline API so that I could add fixes in the process without seamingly changing the object. I started [extracting it in a standalone module](https://github.com/SBoudrias/readline2) this week - up to now, it've only lived inside Inquirer.js – Simon Boudrias Jan 30 '14 at 17:11
  • ah, very nice. Although for future reference: based on that, your post was missing a "node.js" tag and a clarification you weren't working in the browser =P – Mike 'Pomax' Kamermans Jan 30 '14 at 17:16