/    Sign up×
Community /Pin to ProfileBookmark

When to use New

When using functions, for instance hello()

when should I call the script using new [COLOR=”Red”]hello()[/COLOR] or just [COLOR=”Red”]hello();[/COLOR]. I know the new keyword creates a new instance of hello(), but when, in your experience should it be used?

I kind-of know, but I’m not sure if I’m using it right.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@Zaphod42May 04.2008 — since javascript is an object oriented code by design, anytime you want to 'create a new instance' as you said, you need to decide if the instance is a new object, or just a variable of an object....how much informating is being stored in that object, and will it be changed? In my experience the new keyword is most effectively used when creating an instance of something that will have a number of variables. The "hello" example depends entirely on what "hello" does....you CAN use new for that, but is it necessary to create a new instance? will you have both displayed on the page at the same time? or would it be easier to change the properties of an existing hello rather than duplicate it....there is a lot of debate about the "best" way to use javascripts built in functionality...prototype for instance. but when it comes down to it, experimenting, trial and error are sometimes your best bet, because if you are actually writing the code yourself no one can tell you what will work "best" until you've tried it your own way.... in my HUMBLE opinion?
×

Success!

Help @Tabo 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.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...