0

I am puzzled with this one. I have the following code.

var s = "test";
s.len = 4;
var t = s.len;

The question is why the variable t has a value of undefined.

If you check the s.len after that code it is undefined. If you check s the value is test. Not sure what is going on here. I am sure there is an explanation, but can't get my head around that and don't know how to search that.

For those who consider to vote down. This is a question we got in a course, and we are expected to prepare for the next session with this riddle. I am not new to programming, but I fail to research how JavaScripts treats this code. It is valid code really, execute it in your Dev Tools and you will see.

I define a property for the string s called len assign to it the value 4. This property is, I believe created, but undefined. I would like to now why is it ? Is it specific to strings in JavaScript ?

Ely
  • 10,860
  • 4
  • 43
  • 64
  • 3
    `.length`, not `.len` – david May 16 '15 at 11:10
  • 1
    You assign `s.len=4` this is posible only when `s` is a object and `len` is the part of `s` . – Sachink May 16 '15 at 11:19
  • If you wanted to get lenght of string `s` just use `s.lenght`. Why do you assign the value `4` to the string lenght ? – Sachink May 16 '15 at 11:22
  • It is a question we got to prepare and research how objects and so on are treated in JavaScript. @Sachink – Ely May 16 '15 at 11:24
  • Please let us konw what you want to do with JavaScript. We will help you. – Sachink May 16 '15 at 11:26
  • I define a property for the string `s` called `len` assign to it the value 4. This property is, I believe created, but undefined. I would like to now why is it ? Is it specific to strings in JavaScript ? – Ely May 16 '15 at 11:29

3 Answers3

3

but I fail to research how JavaScripts treats this code.

That is easy: strings are primitive types in JS, which means they don't have properties by themselves.

For . operator to work with them (e.g. for .length call) javascript defines such a thing called "wrapper objects".

So when you try to access a property of a primitive object - a temporary wrapper object is created that does behave as an object, hence you can access and assign properties to it.

The problem is that the wrapper object is temporary, so after it's used to access a property the object is discarded with all its state.

That's why when you assign a .len property you cannot access it on the next line: it's lost.

So a pseudo code for what actually happens behind the scenes for your code is

var s = "test";
(new String(s)).len = 4; // here you add an attribute for a new object
                         // and `s` is left untouched
var t = s.len;
zerkms
  • 249,484
  • 69
  • 436
  • 539
2

The question is why the variable t has a value of undefined.

Because you have defined s as a string not as an object. so s.len should be undefined!

I am not sure what are you trying to do. But if you want to get the length of s then t = s.length will simply work.

I define a property for the string s called len assign to it the value 4. This property is, I believe created, but undefined. I would like to now why is it ? Is it specific to strings in JavaScript ?

You can find the answer from this question

Community
  • 1
  • 1
hasan3050
  • 195
  • 2
  • 10
  • Isn't it possible to create a new property to string? – Ely May 16 '15 at 11:31
  • you can follow this http://stackoverflow.com/questions/5201138/why-cant-i-add-properties-to-a-string-object-in-javascript @Elyasin – hasan3050 May 16 '15 at 11:34
  • Perfect answer @hasan3050. Add this to your answer and I accept it right away. That is exactly the explanation I was trying to find. Thanks. – Ely May 16 '15 at 11:42
  • Thank you. I upvoted your answer. However I accepted zerkms answer as it is concise and clear. I hope you understand. – Ely May 16 '15 at 11:48
0

run :

var s1 = "test";
console.log(typeof s1)//string
var s2 = {}
console.log(typeof s2)//object
s1.len = 4;
s2.len = 4;
console.log(s1.len);//undefine
console.log(s2.len);//4
Thomas Zhang
  • 200
  • 2
  • 8