Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have below simple javascript:
function write(msg)
{
document.getElementById("para").innerHTML+=msg;
}
var baz= new function
{
write("abc");
}
And without calling baz, the inner text of para is updated to be "abc".
Why it is being called?
thanks
Because of the new. It treats the function afterwards as a class constructor and calls it to construct a new instance. It might be a but easier to understand if you take:
function Something() {
alert("works");
}
const sth = new Something;
Now just move the function and you got:
const sth = new function() {
alert("works");
};
which is exactly what happens in your case.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
I am trying to assign an IF statement inside a map function but it asks for a return. If I put the return, in front of the IF, does not work. Currently getting the following error on Lint: Line 78:52: Array.prototype.map() expects a return value from arrow function array-callback-return
I expect just to push objects into the array.
What is the correct way to assign the IF inside a map?
{ ingredientIndex.map((currentIndex) => {
const ingredient = detail[`strIngredient${currentIndex}`];
const measure = detail[`strMeasure${currentIndex}`];
if (ingredient) {
validIngredients.push(`${ingredient} - ${measure}`);
}
}) }
ingredientIndex.forEach(currentIndex => {
^^^^^^^
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I was under the impression that the method push works for arrays, not objects... But then why does the following snippet work?
this.usersByMovie = {};
profiles.forEach(profile => {
const movieID = profile.favoriteMovieID;
if (this.usersByMovie[movieID]) {
// the object property is just being accessed using the square brackets
// so `this.usersByMovie` is still an object, not an array
// but why is the `push` method working on this object?
this.usersByMovie[movieID].push(profile.userID);
}
else {
this.usersByMovie[movieID] = [profile.userID];
}
});
this.usersByMovie[id] is actually an array, while this.usersByMovie is an object.
this.usersByMovie[movieID] = [profile.userID];
This line of codes makes this.usersByMovie[id] as an array.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Question: Two Functions Return Different Data But After Calling Both Functions Receive Output From Last Function. Why?
javascript
var printStudentInfo = function () {
return 'Name';
}
var printStudentTerritory = function () {
return 'Age';
}
printStudentInfo();
printStudentTerritory();
To put it simply, Here's what printStudentInfo(); printStudentTerritory(); suggest let someVariable = printStudentInfo(); printStudentTerritory(); Since the last function returns 'age'. Your variable is assigned to 'age'
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to define an async function with an arrow function. This code already throws an error, but I still want to know what the main error is.
async responseName = name => ('hello'+ name);
responseName('joshua').then(response => console.log(response));
You have a syntax error. You cannot define an identifier as async:
const responseName = async (name) => `hello ${name}`;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have created the mapWith function like this:
var mapWith=function(fn)
{
return funtion(list)
{
return Array.prototype.map.call(list,function(something){
return fn.call(this,something);
});
}
};
I use it on a function and an array:
var insertLatLong=function(obj)
{
//inserts to db...
}
var inception_cities=[{lat:35.0117,lng:135.7683},
{lat:48.8567,lng:2.3508},
{lat:-4.0500,lng:39.6667},
{lat:33.8600,lng:151.2111},
{lat:34.0500,lng:118.2500}];
var insertLocations=mapWith(insertLatLong);
insertLocations(inception_cities);
The error I get looks like this:
ReferenceError: list is not defined
at mapWith (/home/anr/Desktop/node js/mysql.js:11:17)
at Object.<anonymous> (/home/anr/Desktop/node js/mysql.js:40:21)
The error is caused because there's c missing in return funtion(list). Without it JavaScript thinks that you want to call something with name funtion. But you also want to pass list to it and since arguments are evaluated first then you get ReferenceError: it does not know what list is.