/    Sign up×
Community /Pin to ProfileBookmark

Wrapper classes

Hi,

General suggestion is not to use wrapper classes like Number,String and Boolean as we can’t modify the Class itself such as adding and removing fields.I think this is mainly because as the wrapper classes are just helper classes which are used to manipulate data, however the structure of the class itself should not change.

I think this is fine right ? We can still use wrapper classes to manipulate the data right ? apart from this is there any other reason for suggesting not to use wrappers.

Thanks
Chandra

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@iBeZiDec 20.2014 — "Wrapper" objects are created when you use a string, number or boolean as an object, i.e. you try to access the "length" property of a string.

They're temporary objects, JavaScript creates them when you access the property, returns the result then immediately destroys the object. This means that any properties you attempt to set on a string won't be saved.

[code=php]
var myString = 'qwertyuiop';

//a new String object is temporarily created using the value of myString, the length property is returned and then the object is destroyed
myString.length;

//a new String object is created and the myProperty property is set, the object is then destroyed
myString.myProperty = 1234;

//another String object is created and we attempt the read the myProperty property, but it doesn't exist
console.log(myString.myProperty);


//what JavaScript is doing behind the scenes when you access myString.length
new String(myString).length;
[/code]


They're mostly used behind the scenes, it's unlikely that you'd want to use them yourself.
×

Success!

Help @Ramu26 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...