Drycodez Posted August 23, 2011 Share Posted August 23, 2011 Is there a way to create a constructor that will return a value just like a string constructor? E.g: str=new String('value');document.write(str)//prints:value. Link to comment Share on other sites More sharing options...
Synook Posted August 23, 2011 Share Posted August 23, 2011 The constructor for a String doesn't return a value; the value of the variable str is a reference to the String instance itself. This is the same as creating a copy of any other prototype: var a = new MyObject(); // a now contains reference to new MyObject instance Link to comment Share on other sites More sharing options...
Drycodez Posted August 23, 2011 Author Share Posted August 23, 2011 How was the 'value' passed into the string func returned? Link to comment Share on other sites More sharing options...
Drycodez Posted August 23, 2011 Author Share Posted August 23, 2011 str=new String('value');document.write(str)//prints:value. Link to comment Share on other sites More sharing options...
jeffman Posted August 23, 2011 Share Posted August 23, 2011 If I understand what you're asking, the built-in JavaScript objects are different from objects you can currently make on your own. Here's what you can do with a string object:var s = "Hi";alert(s); // "Hi"Let's call "Hi" the literal value of s. Maybe there's a real name. I don't know. I think what you are asking is if you can create your own object constructor that returns a literal value the same way a string object does. The answer is no. Maybe in the future. That might be part of the getter/setter suite. I'm not sure.What you CAN do is add properties and methods to the prototype of a string object so that it behaves like a custom object.EDIT:Also, as mentioned many times elsewhere, there is no difference between using a String constructor and simply assigning a literal string to a variable. The objects that are created are exactly the same. Link to comment Share on other sites More sharing options...
Drycodez Posted August 23, 2011 Author Share Posted August 23, 2011 Ok, now i understand! Thanks DD&Synook! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.