/    Sign up×
Community /Pin to ProfileBookmark

Simple Syntax Question

When I define a constructor/class/function in javascript, I can write it as either:

[CODE]var Person = function (name) {
this.name = name;
};[/CODE]

or:

[CODE]function Person (name) {
this.name = name;
};[/CODE]

… Which syntax is the de facto standard? Are there any pros/cons to writing it either way or does it not matter?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscFeb 21.2012 — The second one is the de factor standard....HOWEVER,

from what I've been reading, the first one is becoming more popular, I think mainly as a readability/learning tool.

<i>
</i>(function() {
var Person = 'Me';

function Person() {
//do some stuff
}

console.log(Person);
}());


Run the above code, then comment out var Person and run it again. Both times, Person is defined and gets logged in the console, although the values differ.

By declaring all functions (whether they are constructors for objects or not) as variables, you avoid confusion, although the issue you have brought up has been at the forefront (at least in my readings) in recent months.

I opt for the first method you showed, but, as I said, in most code you'll read you'll probably see the second example.

It'd be interesting to get other peoples' perspectives of this debate as well.

Ultimately, no, it doesn't matter, both pieces of code, for all intents and purposes, do the same thing.
Copy linkTweet thisAlerts:
@WyCnetFeb 21.2012 — Here is how I combine them:


<script type="text/javascript>

function X(a){

this.name=a;

this.fn = function() {alert(this.name)}

}

var n=new X('Sammy');

n.fn();

</script>
[/quote]
Copy linkTweet thisAlerts:
@sh0wtym3authorFeb 21.2012 — Thanks for the feedback. I prefer the latter method as well, but the tutorials I've been reading all seem to use the former.
Copy linkTweet thisAlerts:
@Declan1991Feb 22.2012 — For what I consider actual functions, I always use the latter, unless I want to make a bit of a closure, like:var nextPrime = (function () {
var primes = [];
return function () {
;
};
})();
For objecty sort of things, it depends. If they're in global scope, I tend to use the former a good bit, especially if I consider that a namespace.
×

Success!

Help @sh0wtym3 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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