Both, also an object . Functions, objects, and arrays are all variable types in JavaScript. This code is actually assigning an anonymous function to a variable. Anonymous functions are pretty common in JS, you’ll most often see them used in callbacks.
function someThing(callback) {
callback('I am data!');
}
someThing(function(someData) {
// this is an anonymous function callback for someThing
console.log(someData);
});
There are some benefits and caveats with writing it like this. The first 3 answers of this SO question explain it well:
The Person you have in your code is called a constructor. If you’re familiar with other languages, this is similar to a class definition.
There are some limitations on creating a function that way - the other way using anonymous functions that are assigned as values to variables are far more flexible - particularly when you want to dynamically replace a function with a different function without changing how you reference it.
For example the following code works with both browsers that support JavaScript and old versions of IE that only support JScript (eg IE8).
The first function assigned to addEvent tests whether the browser supports addEventListener or attachEvent. If addEventListener then it assigns a new function to addEvent that uses a JavaScript addEventListener to add the event (the second addEvent function) and ifattachEvent then it assigns a new function to addEvent (the third function) that will work for IE8 instead. The last thing the firsat function assigned to be run as addEvent does is to run the function addEvent which by this time has been replaced by either the second or third functions (whichever version the browser supports). Subsequent calls to addEvent will call whichever of the second and third functions is still assigned to the variable without needing to retest which version the browser supports.
Three anonymous functions all assigned to the same variable in such a way as to only need to test browser support once. You couldn’t have three different functions sharing the same name if you define the functions the alternative (and less flexible) way.
Another even more common use of anonymous functions is wrapped around all modern JavaScript to avoid having it able to clash with any other script.
(function() {
"use strict";
// all JavaScript for the script goes here
// where it is completely separate from all other scripts
}());
One other small point on this, it’s considered good practice to name object variables with their first letter capitalised, where other variables are all lower case - it helps differentiate them visually whilst working with the code.
[quote=“chrisofarabia, post:4, topic:211466, full:true”]One other small point on this, it’s considered good practice to name object variables with their first letter capitalised, where other variables are all lower case - it helps differentiate them visually whilst working with the code.
[/quote]
That’s interesting. My understanding of capitalising the first letter is that it was only for constructor functions.
For example:
function Person(name) {
this.name = name;
}
var person = new Person('Joe Bloggs');
Is the expansion to also include object variables represented in any of the commonly used style guides, such as the ones found at JavaScript Style Guides And Beautifiers ?
I’m gong to disagree with @felgall here, no those are different.
x1 has a reference to a new object
x2 has a reference to an anonymous function that returns undefined
Run the following code in a terminal to see the difference.
var x1={};
var x2=function(){};
console.log(x1);
console.log(x2);
console.log(x2());