2

I can't figure out myself why foo.bar in the example is undefined, could you explain?

var foo = "foo";

foo.bar = "baz";
console.log(foo.bar); // undefined

Q2: How do I add references to properties and methods to the String instance foo?

Help is appreciated, thanks.

-- EDIT --

Note: the question is about a generic String instance, not the String global object. So using "classic" prototyping as someone suggested is not an option, because this way every String instance would have a property called bar, while I want to augment only certain instances.

Paolo
  • 20,112
  • 21
  • 72
  • 113
  • 1
    possible duplicate of [Why can't I add properties to a string object in javascript?](http://stackoverflow.com/questions/5201138/why-cant-i-add-properties-to-a-string-object-in-javascript) – Quentin Apr 11 '11 at 18:20

6 Answers6

3

This is a duplicate of Why can't I add properties to a string object in javascript?.

Basically, you're creating a string, and this is a primitive type in javascript.

You cannot add properties to primitive types in javascript.

Community
  • 1
  • 1
typeof
  • 5,812
  • 2
  • 15
  • 19
2

foo.bar = "baz"; is the same as undefined = "baz";

You can add functions to string by using it's prototype;

String.prototype.bar = function() {
    return "baz";
};

foo.bar() ; //# => baz
Raynos
  • 166,823
  • 56
  • 351
  • 396
James Kyburz
  • 13,775
  • 1
  • 32
  • 33
2

By using a real String object for foo:

var foo = new String('foo');
foo.bar = 'baz';
console.log(foo.bar); // 'baz'
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • +1 Tested this in rhino and it works. Though `'s' === new String('s')` evaluates to false as they have different types. Sort of thing I could imagine causing bugs. – Dunes Apr 11 '11 at 21:49
  • Don't know about bugs, I think if it's badly used it can cause bugs. But hey, that's with all code, isn't it? Anyway, it's all doable but I have never found much use for this kind of string-extension, I must say. – KooiInc Apr 12 '11 at 05:12
1

When you specified var foo = "foo"; you are asking foo to be interpreted as a string. String can only have a literal as a value. It cannot have any other sub-properties. ( just extend this logic to any other oo programming language you know and it will become clearer). Instead you could do something like this.

 var fooObject = new Object()
fooObject.foo = "foo"
fooObject.bar = "baz"
uncaught_exceptions
  • 21,712
  • 4
  • 41
  • 48
0

You're talking about "prototyping", which is the ability to add custom properties to objects in javascript.

This is an excellent and concise tutorial on prototyping: http://www.javascriptkit.com/javatutors/proto.shtml

DWRoelands
  • 4,878
  • 4
  • 29
  • 42
0

To extend the String class, modify its prototype:

String.prototype.bar = "baz";
var foo = "foo";
console.log(foo); // "foo"
console.log(foo.bar); // "baz"

In your question, you were modifying an instance of the String class instead of the String class itself.

Brad
  • 5,428
  • 1
  • 33
  • 56