Why is `{foo: function myName() {}}` acceptable syntax? [duplicate] - javascript

This question already has answers here:
Why use named function expressions?
(5 answers)
Closed 8 years ago.
Is there a different outcome (scope etc...) between the following two function declarations?
var myObj = {
foo: function myName() {}
}
var myObj = {
foo: function() {}
}

No, the scope is the same. In javascript anonymous functions can be named and its good practice to do so because when debugging they are named in stack traces. Also you can refer to the function inside itself. More details here.

when you give a name to the function, it's only available from inside that function
var myObj = {
foo: function myName() {
myName()
}
}

Related

Outside scope this key word in JavaScript? [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 5 years ago.
I am not sure why I'm getting undefined with the code below. I tried to declare a variable under the say method var _this = this and then console.log out _this.name but it did not work.
let dog = {
name: 'doggo',
sayName() {
console.log(this.name)
}
}
let sayName = dog.sayName
sayName()
window.name="test";
sayName();//test
Executes the function in window context so this is window. You may want to keep the dog context either through passing it:
sayName.call(dog);//doggo
Or through keeping a bound function:
let sayName = dog.sayName.bind(dog);
sayName();//doggo

Does the way a JS function is defined affect it's "performance"? [duplicate]

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.

javascript function declarations and differences in scope [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
What is the difference between a function expression vs declaration in JavaScript? [duplicate]
(5 answers)
Closed 8 years ago.
I couldnT find an answer easily, so even if this question is a dupe, the answers donT come up using these keywords.
I want to know the difference between the different way of declaring functions in a sample app.js
var foo = function()
{
//..
}
function bar()
{
//..
}
var baz= function()
{
//..
}
function qux()
{
//..
}
// other??
I m also not clear about the scope where I can use each function. Thanks!
There are four ways to create a function in JavaScript.
Function declaration
This will create a variable foo in the current scope and assign a named function to it.
function foo () {
}
Function declarations are hoisted so it doesn't matter where, in the applicable scope, you put them. It is considered good coding practise to define them before you use them though.
Anonymous function expression
This will create a function without a name and use it in an expression. In this example it is assigned to the variable something.
something = function () {
};
Named function expression
This is the same as an anonymous function expression except that it has a name, creates a variable of that name in the scope of itself and is horribly broken in older versions of Internet Explorer.
something = function foo () {
};
Function constructor
Do not use function constructors. They are eval by another name. You can read about them on MDN if you're interested.

JavaScript function declaration difference [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}
What's the difference in declaration of this functions i know example one is the normal way to do it, why we need two and three?
function one(var1,var2) {
alert("inside functtion one");
}
two = function (var1,var2) {
alert("inside function two");
}
var three = function (var1,var2) {
alert("inside function three");
}
The first and the third are just two ways to declare a function which exists globally in the scope chain. The middle is attaching the function two to the window object and allowing it to exist there.
console.log(window.one); // undefined
console.log(window.two);
console.log(window.three); // undefined

Why are anonymous functions treated differently from named functions here? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Javascript: var functionName = function() {} vs function functionName() {}
What is the difference between a function expression vs declaration in Javascript?
Today I stumbled upon the following phenomenon:
foo();
bar();
function foo()
{
console.log("inside foo");
}
var bar = function()
{
console.log("inside bar");
}
FireBug complains with the following error message:
bar is not a function
Several tutorials claim that function f() and var f = function() are basically the same thing. Evidently, they are not, but what exactly is going on here?
Function declarations are available anywhere in the scope they're defined in, even before their physical definitions.
var bar = function() { ... }; is a normal variable that happens to hold a function. Like all other variables, it can only be used after its assigned.
(You cannot observe the future value of a variable)

Categories

Resources