52 questions
-1
votes
3
answers
102
views
Cypress Error: 'cy.within() can only be called on a single element'
I've received a Cypress error suggesting that multiple elements contain the searched value, so it cannot recognize the requested one.
Here is the Cypress script I've used for testing:
it('Check all ...
0
votes
1
answer
45
views
Why order of sloppy-mode function statement in block affects global variable differently?
snippet 1 -> output is "b"
if (true) {
x = "b";
function x() {}
}
console.log(x); //b
snippet 2 -> output is ƒ x() {}
if (true) {
function x() {}
x = "b";...
-2
votes
2
answers
127
views
Can some please explain to me the difference between the function keyword and const in declaring a function [duplicate]
As demonstrated in the below examples, I used the function keyword to declare a function that will generate a random integer number within a range, and I also used a const keyword to declare another ...
1
vote
0
answers
41
views
JavaScript Immediately Invoked Function - Distinguishing Between JavaScript Function Expressions and Declarations [duplicate]
In my journey of learning JavaScript, I encountered my first practical use case for an Immediately Invoked Function Expression (IIFE).
However, I'm struggling to understand the distinction between a ...
0
votes
1
answer
113
views
Does hoisting of functions behave differently when strict mode is off? [duplicate]
Here are two similiar codes except for the "use strict" part
"use strict"
{ function a() {
return 1;
}
}
function a() {
return 2;
}
...
0
votes
2
answers
157
views
Javascript Hoisting - How are we Accessing Certain Variables?
I have two questions about hoisting:
The way function declarations are hoisted is that they go to the very top, even above variable declarations, to my understanding.
If we have this code:
function fn(...
2
votes
0
answers
53
views
collapsing function and variables name [duplicate]
Here is the fact in Head first js,
when the browser evaluates a function declaration, it creates a function as well as a variable with the same name as the function, and stores the function reference ...
0
votes
4
answers
348
views
Function works only in Safari, other browsers throw `Uncaught TypeError: X is not a function`. Where's the problem?
I implemented a new feature to our CRM and everything works as it should on Safari (macOS), but it throws Uncaught TypeError: X is not a function on every other browser we tested it on (Chrome, ...
1
vote
0
answers
59
views
Invoking function declaration in JavaScript [duplicate]
I have a doubt regarding how the function declaration are invoked in JavaScript.
I have read somewhere that function declaration can be accessed anywhere within the function it was declared on.
Lets ...
0
votes
2
answers
221
views
JavaScript hoisting for function expression and function declaration [duplicate]
I am learning hoisting in JavaScript and practicing some examples.
In an example I am confused of it's output.
Here is the example code:
var f = function(){
console.log("Expression");
}
...
0
votes
2
answers
66
views
Are these 2 JavaScript statements equivalent? [duplicate]
These 2 statements seem to do the same thing.
const handleClick = () => alert('foo');
and
function handleClick() {
alert('foo');
}
Are they identical and just syntactically different? The ...
2
votes
1
answer
1k
views
Why arrow function has to be declared above the caller function
In a javascript file, when I declare a function using function keyword, I can placed my function after my caller function, something like
// test.js
function myCaller() {
foo('hello world'); ...
0
votes
1
answer
79
views
toggling variable inside Module Pattern using function declaration vs function expression
I have a module pattern with a variable setting the currentPlayer to 1. I use a function expression to toggle that variable
const game = (() => {
let currentPlayer = 1;
const ...
1
vote
1
answer
62
views
Is an anonymous function as a parameter a function declaration or a function expression?
Assuming a function declaration is a statement where the function keyword is the first word of the statement, e.g.:
function() { console.log("foo") };
Assuming that a function expression is e.g. ...
0
votes
1
answer
4k
views
Why do function declarations get hoisted and function expressions don't?
According to hoisting definition:
Hoisting is a JavaScript mechanism where variables and function
declarations are moved to the top of their scope before code execution
Why do function ...
1
vote
1
answer
158
views
Why Named Function Expression itself cannot assign Name to another Value? [duplicate]
var functionVariable = function functionExpressionName() {
functionExpressionName = 1;
console.log(functionExpressionName) // function
};
functionVariable();
If You run this example you can ...
1
vote
1
answer
83
views
When and why to use these various Java Script function declaration formats? [duplicate]
What are the differences between the following function declaration formats?
When is it more correct to use which?
From a beginner's perspective, and a high level (non-deep) point of view, they all ...
0
votes
2
answers
88
views
Recursion with function declaration JS
There are many questions about recursion with function expressions. And there are two ways to do it: One is using named function expression and second is using arguments.callee. But at this time ...
1
vote
1
answer
53
views
Why is it possible to assign to properties of a function before its declaration?
This piece of code
uniqueInteger.count = 0;
function uniqueInteger() {
return uniqueInteger.count++;
}
console.log(uniqueInteger());
console.log(uniqueInteger());
produces the following ...
1
vote
2
answers
458
views
Function declarations precedence/overwriting variable declarations? Hoisting? Why? [duplicate]
Snippet 1:
var a; // undefined variable named 'a'
function a(foo) { // a function named 'a'
var foo = "Hello World";
console.log(foo);
}
console.log(a); // output is: [Function: a], but ...
1
vote
2
answers
59
views
Is it possible to have parameters for anonymous function?
Given
var stuffs = [
{ id : 1, name : "orange"},
{ id : 2, name : "apple"},
{ id : 0, name:"grapes"}
];
var filterMethod1 = new function(o){return (o.id>=1);}; // this gives undefined ...
1
vote
1
answer
156
views
Does a function's name act as a variable?
I have a question about this code below:
function myfunc () {
return 2 + 2;
}
console.log(myfunc);
Does anyone know why, when we log 'myfunc' to the console, we get the entire function itself back? ...
0
votes
2
answers
40
views
Does assigning to a function overwrite the function or create an implicit global?
In JavaScript we can declare a function and then assign to it, as follows:
function spam() { return "spam 'n eggs"; }
spam = spam();
spam(); // TypeError: spam is not a function.
Does this code ...
0
votes
3
answers
449
views
Javascript: Global Context and Function Context Declarations [duplicate]
I have noticed something while playing around which has sparked a quick question.
When code is executed in the global/window context, any function declarations get added as methods to the window ...
-1
votes
1
answer
91
views
Are all functions inside of an IIFE expressions?
If (function foo(){}) is an expression due to the 'context' as "(Parenthesis)" are a grouping operator and grouping operator can only contain an expression.
Which leads to the question, can you ...
1
vote
1
answer
175
views
Closure and function hoisting- not working on firefox
The following code gave an error, on some version in firefox browser - linksHandle is not defined.
The code is comprised of a function that at the bottom has a function named linksHandle. As far as I ...
13
votes
4
answers
11k
views
Javascript function declaration with same arguments
I am learning javascript myself. I found if I declare a function with same arguments it just working fine:
function func(a, b, a){
return b;
}
alert(func(1,2,3));
But if I do this :
function func(...
1
vote
0
answers
45
views
In JavaScript, Why function expression needs to be in bracket for calling the function [duplicate]
I tried calling
function(v){alert(""+v);}(4);
But warning came out
function statement requires a name
When I called using name
function fname(v){alert(""+v);}(4);
Nothing happenedBut when I called ...
3
votes
1
answer
126
views
Why doesn't the location of a function expression matter in node js?
In JavaScript, when using a function expression (e.g. var myFunc = function() {...}), like any other variable declaration, you have to define the variable before using it. For example, the following ...
1
vote
1
answer
71
views
What are good situations to use function expression instead of function declaration?
I will prefer to use function declaration all the time because I can place the function anywhere on the source file. If I use function expression, the function has to be placed at the top of the ...
1
vote
3
answers
253
views
Javascript private methods: function expression vs function declaration [duplicate]
A common method of creating private methods (of sorts) in JavaScript is this:
Class = function (arg0, arg1) {
var private_member = 0;
var privateMethod = function () {
return ...
0
votes
1
answer
1k
views
JS Function declarations should not be placed in block
I have wrapped 95% of the 'parentCtrl' (Controller) within an 'IF' statement, this prevents any functions from triggering if the user isn't signed in and authorized. Since doing this I keep getting ...
1
vote
1
answer
177
views
JavaScript: Change function declarations to expressions at legacy code
I'm refactoring legacy code. I can see that for functions used both declarations and expressions. I know that declarations are hoisted, expressions work as step-by-step execution. There are also named ...
38
votes
2
answers
12k
views
What are the precise semantics of block-level functions in ES6?
I'm trying to wrap my head around the new standardized block-level functions in ES6 by reading the raw spec. My superficial understanding was:
Block-level functions declarations are allowed in ES6.
...
1
vote
2
answers
1k
views
Function declaration vs expression from a performance point of view?
There's many javascript articles on the web comparing function declarations function foo (){} to function expressions var foo = function () {}. They usually mention things like hoisting, behaviour ...
0
votes
2
answers
337
views
Function returning a variable without declared it
I recently search in the code of the library of knockout to find how observables are able to create dependencies with computed functions when we call it.
In the source code, I found the function ...
4
votes
3
answers
1k
views
In JavaScript, why can't I immediately invoke function declarations?
Only functions expressions can be immediately invoked:
(function () {
var x = "Hello!!"; // I will invoke myself
})();
But not function declarations? Is this because function declarations ...
1
vote
3
answers
3k
views
All JavaScript Function Types?
While making several test projects for my small library of code I have come across many tutorials that go about making functions in many different ways.
For example:
Function Declarations
...
5
votes
1
answer
1k
views
Function Declaration vs Function Expression in the Module Pattern [duplicate]
I have just learned about the difference between Function Declarations and Function Expressions. This got me wondering about whether or not I'm doing things right in my AngularJS code. I'm following ...
1
vote
1
answer
84
views
Do function declarations in a prototype "pollute" the prototype?
What is the difference between this:
Library1 = function () {};
Library1.prototype.myFunc = function (p) {
function helper1(p) {return p * 2; }
function helper2(p) {return p * 4; }
this....
1
vote
1
answer
501
views
Why can I not pass a "function declaration" as an argument in an event handler function with JQuery?
Why isn't this working using function declaration, but it's working perfectly using function expression?
Assuming that the only difference is how the browser loads them into the execution context.
...
0
votes
1
answer
146
views
Caller property of JS for "foo = function()" style of coding [duplicate]
I want to use the property of "caller" for a function which is defined here
It works fine for this style of function declaration
function g() {
alert(g.caller.name) // f
}
function f() ...
2
votes
2
answers
74
views
Are all function declarations & expressions created by called new Function() behind the scenes?
I'm reading the portion of ECMA 262 v5 script that speaks of Function definitions. For both function declarations and function expressions, the following is mentioned:
Return the result of creating ...
6
votes
1
answer
238
views
Function declaration or function expression
I just ran into a problem when defining a function in a block scope. Consider the following program:
try {
greet();
function greet() {
alert("Merry Christmas!");
}
} catch (error)...
4
votes
3
answers
247
views
Why is declaring a function at global scope not equivalent to assigning a function to a global-scope binding?
I saw this on Twitter and I couldn't explain it either. Defining a onload function in the following two manner works:
onload = function(){
console.log('this works');
};
window.onload =...
0
votes
2
answers
265
views
Determine if JavaScript code is an expression
I'm using the following function named isExpression to determine whether some JavaScript code is an expression:
function isExpression(code) {
try {
new Function("return " + code);
...
3
votes
2
answers
4k
views
How do browsers handle multiple function declarations with the same name?
How do browsers handle multiple function declarations with the same name?
Specific test case is below - NOTE: I know this does not make sense to allow a server script to create more than one function ...
80
votes
7
answers
39k
views
Function declaration in CoffeeScript
I notice that in CoffeeScript, if I define a function using:
a = (c) -> c=1
I can only get the function expression:
var a;
a = function(c) {
return c = 1;
};
But, personally I often use ...
90
votes
4
answers
21k
views
JavaScript function declaration and evaluation order
Why does the first one of these examples not work, but all the other ones do?
// 1 - does not work
(function() {
setTimeout(someFunction1, 10);
var someFunction1 = function() { alert('here1'); };
})()...
2
votes
1
answer
350
views
In Javascript, what is the motivation or advantage of using var foo = function foo(i) { ... }? [duplicate]
I see that in the answer of
In Javascript, why write "var QueryStringToHash = function QueryStringToHash (query) { ... }"?
which is doing something like
var foo = function foo(param) {
...