This question already has answers here:
What is the difference between a function expression vs declaration in JavaScript? [duplicate]
(5 answers)
var functionName = function() {} vs function functionName() {}
(41 answers)
"new" operator before declaring function
(2 answers)
Closed 7 years ago.
I'm having a hard time understanding the difference between these two functions. Are they the same? It seems like in the first case, appleOne is the name of the object, and in the second case, appleTwo is the name of the function. However, I read that functions and objects are the same in Javascript, so I'm confused...
var appleOne = new function(color) {
this.color = color;
}
function appleTwo (color) {
this.color = color;
}
Reference: Code from http://www.phpied.com/3-ways-to-define-a-javascript-class/
The difference is that the object associated with the variable appleTwo is a function object, which the object associated with the variable appleOne is not a function: it is a "regular" object with the field color.
I wouldn't say that "functions and objects are the same" in JavaScript. What is true is that there are several kinds of objects in JavaScript:
regular objects
arrays
functions
regular expressions
In the first example, you used an anonymous function as a constructor, so the object you produced and assigned to appleOne is a "regular" object. The second example uses a function declaration to define a function.
If your question was not about the difference, but rather why the first case "works" (because it is not a very common pattern), there are several S.O. questions available with the answer.
Related
This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Methods in ES6 objects: using arrow functions
(6 answers)
Closed 4 years ago.
In ES5, other properties in object can be accessed by using this keyword.
Same behavior but can't able to access other property in object by using this in arrow function (ES6)
Kindly run attached code snippet see the output
var person={
"firstName":"PraveenRaj",
"lastName":"D",
"getFullName": function() { return this.firstName+" "+this.lastName}
}
var person1={
"firstName":"PraveenRaj",
"lastName":"D",
"getFullName": () => this.firstName+" "+this.lastName
}
console.log(person.getFullName());
console.log(person1.getFullName());
Like all JavaScript variables, both the object name (which could be a normal variable) and the property name are case sensitive. You access the properties of an object with a simple dot-notation.
Following is the syntax for accessing Object Properties.
objectName.propertyName
An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.
Credits: Docs, Reference Answer
This question already has answers here:
Calling function inside object using bracket notation
(2 answers)
Closed 6 years ago.
I rather have a seemingly trivial issue, but am not able to figure out an efficient approach.
I have a list of about 50 functions to be called such as :
globalClient.funcA(...)
globalClient.funcB(...)
globalClient.funcC(...)
My code should ideally dynamically create the name of the function (funcA / funcB/ funcC and then proceed to actually call that function. My approach below does not work (please note that these aren't exactly the actual names of the functions. I'm only giving these arbitrary names for simplicity of understanding):
var functionName = 'func'.concat('A');
globalClient.functionName
The second line is where it errors out. Now JS thinks that functionName itself is the name of the function. What I want it to do is resolve functionName to funcA and then call globalClient.funcA(...) instead.
I've thought about implementing a switch / case for this but I'm sure there is a far simpler appraoch. Any ideas?
You could use the bracket notation as property accessor.
globalClient[functionName]()
You can use the [ ] operator for accessing the properties.
var globalClient = {
funcA: function(){
console.log('funcA is called');
}
}
var functionName = 'func'.concat('A');
globalClient[functionName]();
This question already has answers here:
Why use named function expressions?
(5 answers)
Closed 6 years ago.
In some projects I can see, that the functions wich are object methods get names after the function constructor - I can not see why, can any one explain?
Example: named
someObj.prototype = {
load: function someObj_load(file) {
vs unnamed
someObj.prototype = {
load: function(file) {
I can not see any advantage in the above.
So you can see the name of the function name instead of Anonymous function in stack traces. I think some browsers will pick up the name of the variable/attribute you've assigned it to. Some don't.
This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 7 years ago.
Is there ANY difference in the following two ways of defining a functions?
METHOD 1)
var printName = function(name){
return("Hi! My name is ",name)
}
VS
METHOD 2)
function printName(name){
return("Hi! My name is ",name)
}
and I mean ANY, I'm new at JS and want to lay down my understanding of functions and Objects before I advance as I feel these 2 features are the 2 I'll use the most.
Yes there is a difference, but none that would affect the performance of the function code when it's called.
The difference has to do with when the function is created, but the performance is identical. Using your examples:
printName_1("Drew"); // This will fail, as printName_1 is not defined (yet)
printName_2("user4820485"); // This will work
var printName_1 = function(name){
return "Hi! My name is "+name;
}
function printName_2(name){
return "Hi! My name is "+name;
}
Functions that are declared using the latter syntax are initialized at the beginning of the block where they appear, so it looks like they can be called before they are defined.
This question already has answers here:
What is meant by 'first class object'?
(11 answers)
Closed 7 years ago.
Can someone explain this term for me and describe a typical programming situation where first-class functions are used?
Thanks
We often hear that JavaScript functions are first-class functions, meaning both functions and objects are treated by the language as the same thing. In practical terms, a function can be stored as a variable, inside an array or an object, as well as it can be passed as an argument or be returned by another function. That makes functions “first-class citizens” in JavaScript.
These are the examples:
var myfunc2 = function(a)
{
return a + 1;
};
var myfunc2 = function myfunc4(a)
{
return a + 1;
};
Refer the following links
http://odiseo.net/javascript/first-class-functions-in-javascript-how-comes-functions-are-treated-as-objects-in-js
http://www.developerfusion.com/article/84433/first-class-functions/