Questions tagged [function-expression]

100 questions
3
votes
1 answer

function expression on firefox - not expected result

if (true) { function foo(){ return 1; } } else { function foo(){ return 2; } } foo(); The above code is an example of function expression and returns 1 in Firefox 28 whereas 2 in Chrome ( expected result). Why is firefox giving wrong result ?
Vivek Chandra
  • 4,240
  • 9
  • 33
  • 38
3
votes
1 answer

Javascript: Define variable as anonymous function that calls the same function

I am given a function like this: //! Work on record. /*! \param[in] Record (object) Record. \param[in] AsyncCallback (function) Asynchronous callback which is called when the operation is complete. It takes no…
2
votes
2 answers

When we should use class expression in Javascript?

I have recently got to know that we do have class expressions too like function expressions in JS. I know some points about it(MDN) like: Class expressions may omit the class name ("binding identifier"), which is not possible with class…
Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
2
votes
1 answer

How do you check for null or undefined on a variable assigned to a missing anonymous function expression?

When commenting out the anonymous function expression "a" below, how do you properly check for NULL on "b"? Use case: Lets say "a" is an in-page JS include (library) that is assigned to "b", but the library failed to load and now we have a console…
2
votes
1 answer

Reference to an object's array from within the object in Javascript

I would like to create a new object of the type InvertedPeninsula: var invertedPeninsula = new InvertedPeninsula(); To create this object type, I use an object constructor function: var InvertedPeninsula = function() { this.inhabitants = [ …
Dean P
  • 1,841
  • 23
  • 23
2
votes
1 answer

How to prevent JS minification from removing names of named-function-expressions?

I have this function on an object that I need to trace REALLY BADLY, along with the parent caller of the invocation and arguments passed to the caller. This well works until minified: var foo = { FunctionToBeLogged: function GiveMeAName() { …
2
votes
3 answers

What is the preferred way to use a Javascript function expression?

I've seen these different patterns when calling a JavaScript function expression: Pattern #1 var global = function () { return this; }(); Pattern #2 var global = (function () { return this; }()); Pattern #3 var global = (function () { …
1
vote
3 answers

Passing a function expression to be used as onclick handler for a button

For a button, I need to set the function this calls, and the value used in that call, at runtime. I can do this like so: var myfunction = /* ... */ var myvalue = /* ... */ button.setAttribute ("onclick", myfunction + "('" + myvalue + "')"); If I…
gzost
  • 2,375
  • 1
  • 18
  • 25
1
vote
1 answer

Can You Implement Javascript Class Via Function Expression?

I was tackling this question in leet code: Implement the MapSum class: MapSum() Initializes the MapSum object. void insert(String key, int val) Inserts the key-val pair into the map. If the key already existed, the original key-value pair will be…
maddie
  • 1,854
  • 4
  • 30
  • 66
1
vote
2 answers

What is the difference between 'let' and 'this' in JavaScript?

I'm learning JavaScript, coming from Ruby, have done some stuff in C as well. One example in my reading is: function letCounter() { let counter = 0; return () => ++counter; } and is compared to function thisCounter() { this._count =…
1
vote
1 answer

Console output sequence sometimes does not match with the sequence in which they are called

I am new to JavaScript and this is my first question on Stackoverflow, please let me know if I missed something which should be included in the question. Question 1: Can anyone explain why sometimes the console output sequence does not match…
AtlasUser5
  • 81
  • 6
1
vote
4 answers

How to use function expression values in other functions?

I'm learning Javascript and I make a rock, scissors, paper project. I already make it work with the sample code but now I want to use a function expression to use its value in other functions. When I load the code it gave me undefined and I don't…
user12114366
1
vote
1 answer

Why Named Function Expression itself cannot assign Name to another Value?

var functionVariable = function functionExpressionName() { functionExpressionName = 1; console.log(functionExpressionName) // function }; functionVariable(); If You run this example you can see we can not reassign to functionExpressionName…
1
vote
1 answer

JS Class: Difference between ES6 myFunc(){..} and ES5 myFunc = function() {...} in a class declaration

In the following code, class PersonClass { constructor(fname) { this.fname = fname; } read = function() { console.log('I am reading') } speak () { console.log('I am speaking'); } } //Instantiate let p1 = new PersonClass('Raj') read…
appu
  • 471
  • 1
  • 8
  • 26
1
vote
0 answers

Assigning arrow function as the default value with ||

So I was writing a function which would take an argument(another function), which when undefined would be assigned a default value. Assigning that default value as an ()=>{} does not seem to work. However when I wrap the default function in (()=>{})…