Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Below is the simple nested function I wrote in Javascript
function outside(x) {
function inside(y) {
console.log(x + y);
}
return inside ; //
}
const fnInside = outside(3);
const result = fnInside(5);
The programme is behaving normally. However , if I remove return statement from line 5 , it throws below error.
I am new to Javascript.Can anyone please explain to me why return statement removal makes programme behave like this ?
Thank You
If you remove return you will not return the function reference and the outer function will be void function which will return undefined by default, and when you try to execute undefined as a function it will give an error that undefined is not a function to be executable.
Same as the snippet below.
const undefinedValue = undefined;
undefinedValue()
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Is it possible to manage object's properties via set get in defineProperty?
I'm not sure that I'm using this sentence properly.
<div id="app"></div>
why
<script>
var div = document.querySelector('#app');
var viewModel = {};
Object.defineProperty(viewModel, 'str' , {
get: function() {
return console.log("access");
},
set: function() {
return console.log("setting");
}
})
</script>
I assume that you have run viewModel.go in console - you will get "access" printed in console, but later you will get undefined as it is a result of this get function:
function() {
console.log("access");
}
This function doesn't have a return clause, so value of go will be undefined.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've got an object.
function Obj()
{
}
Obj.prototype.doSomething = function(thing)
{
this["do" + thing]();
}
Obj.prototype.doAlert = function()
{
alert("Alert!");
}
var obj = new Obj();
obj.doSomething("Alert");
This is just a shortened down version of my object, and is a lot bigger.
What I would like to do is that if you pass in 'Alert' it will run this.doAlert(); and if I pass in 'Homework' it will run this.doHomework();
Obviously, in this case, it is stupid to do it like this, but my final project is going to be completely different.
It works fine with window like this:
window["do" + thing]();
but I don't want it to be a global function, but to be part of obj.
Does anyone have an idea how I'd go about doing this?
Thanks in advance!
It turns out that when you get the function through this['functionName'], this is not bound to it.
This means you cannot use this.foo inside any function called like above.
To fix this, I used the following code:
this["do" + thing].bind(this)();
instead of this["do" + thing]();
JSFiddle: https://jsfiddle.net/auk1f8ua/
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm calling a function like this:
$('.qa-nav-main-ask a').click(showAskForm);
Show showAskForm is this (simplified):
function showAskForm(){
return true;
}
I want to catch the response from showAskForm and save it to a variable, I tried
askFormShowing = $('.qa-nav-main-ask a').click(showAskForm);
without success.
function showAskForm(){
return true;
}
var askFormShowing;
$('.qa-nav-main-ask a').click(function(){askFormShowing = showAskForm();});
Try the above
return is a reserved word and cannot be used as a JavaScript identifier. So, use like this:
$('.qa-nav-main-ask a').click(showAskForm);
function showAskForm(){
myvar = true;
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I was wondering which of the two is appropriate:
error = null or error = false
if(error) {
//handle it
}
Is it a matter of preference/coding style or does one have an actual advantage over the other. Is it contextual? Is there a consensus at all on the issue?
Thank you.
Both the examples you have given will evaluate to false.
In this simplistic example you have given there is no difference - both null and false will trigger the conditional expression.
Let me try again.
error = null;
if (!error === true) {
// this will work
};
if (error === false) {
// this will not work
};
Here is a fiddle you can test in.
Both statements evaluate to false.
var error = false; will be appropriate if you only want to know about error or not. If some cases, you may want to have error message in the error, then you can assign to null.
Both are valid. The choice is based on purpose of error variable.
I'd rather use the error variable as a string, then you can add messages to help debugging, telling the developer where the problem occured.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code:
replaceAny('this','that',string);
if(val!="")
the replaceAny function looks like this:
function replaceAny(first,second,ele) {
var val = ele.replace(first,second);
alert(val);
return val;
}
But then after running the replaceAny function (and the alert shows the right value, the if condition tells me that the variable val is not set, why?!
You are not using the return value from the function correctly. This is something like what you want
function replaceAny(first,second,ele) {
return ele.replace(first,second);
}
var val = replaceAny('this','that',string);
if(val!=""){
//something, something darkside
}
I think you are not assigning the value returned by your function to your variable val.
Try like this
val = replaceAny('this','that',string);
if(val!="")