This is a part of my document:
function MyFunction() {
var x=""
if (x=1) {
OnBtnPbDemo_SwitchChn1(1); //This is a function
} else {
OnBtnPbDemo_SwitchChn1(0); //This is another function
}
}
I want to know if this is the right way to call the functions inside the condition.
Thank you very much.
Not entirely sure what you mean about the "right" way of calling, but you can call functions wherever as long as they're available in the scope.
You can actually shorten what you've written to this too:
function MyFunction () {
var x = "";
OnBtnPbDemo_SwitchChn1(x === 1 ? 1 : 0);
}
Unless you're actually changing the x variable inside your function however, it'll never run with 1 as the param.
Yes, calling functions is the same no matter where you are calling it.
You need to use == in if condition instead use of =
if (x==1) {
instead of
if (x=1) {
if you are going to call same function for the different x value, Try this
function MyFunction() {
var x = 1;
OnBtnPbDemo_SwitchChn1(x); //you can pass the x value directly to that function.
}
if you are going to call different function for the different x value, Try this
function MyFunction() {
var x="";
if (x==1) {
OnBtnPbDemo_SwitchChn1(1); //This is a function
} else {
OnBtnPbDemo_SwitchChn1_another(0); //This is another function
}
}
You are calling same function twice, Instead just call the function once with the value 1/0 in it.
function MyFunction() {
//Check and find value of x
if(x=="somevalue") //true condition
{
x=1;
}
else{
x=0;
}
OnBtnPbDemo_SwitchChn1(x);
}
Related
i have 2 different javascript environments and want to create a library usable on both. to do that i want to use higher order functions to generate my functions, like so
try {
Maptool.chat.broadcast("")
var myFunc1 = genFunction1("env1");
}
catch(e){
if(e instanceof ReferenceError){
var myFunc1 = genFunction1("env2")
}
}
genFunction1 would return a function that i could call built from a function expression. there are only 2 ways i know to generate the function that is returned, it could be written similarly to the next code block with the if outside the inner function which would create a lot of code duplication as i would need to write everything in the function that is unchanged again.
function genFunction1(env){
if(env == "env1"){
return function () {return 3;}
}
else{
return function () {return 2;}
}
}
or with the if inside like the following code block
genFunction1(env){
return function (){
if(env=="env1"){
return 3;
}
return 2;
}
}
however if we use a object with a get property which logs when accessed we can see that the third code block calls a if for each call of myFunc1.
function genFunc1(obj){
return function (){
if(obj.env == "env2"){
console.log("3");
}
console.log("2");
}
}
const obj = {
get env() {
console.log("accessed property");
return "env2";
}
}
var myFunc1 = genFunc1(obj);
myFunc1();
myFunc1();
as previously said the earlier code block calls the get method once for each call of myFunc1(),but the environment won't change aside from the first time i check, is there any way to make the generated function not include a if and include the if only in the higher order function without duplicating code?
This is expected behavior as you are getting property from an object. Take a snapshot of the value before generating the function, like this.
function genFunc1(obj) {
var env = obj.env
return function() {
if (env == "env2") {
console.log("3");
}
console.log("2");
}
}
I want to do something like this:
Say I already have a function named function()
If (function() == 2) {
console.log("you are great")
}
How do I do this the correct way?
You can't have function named function in JavaScript - it is reserved word. Fix to your problem would be give function a specific name when declaring it.
function test() {
// just example
return 2;
}
Then you can use it in if condition:
if (test() === 2) {
// do something
}
As mentioned in comments you have to name your function correctly (function is a reserved word in JS) and then check your condition in if statement. Here you can find simple example:
function getA() {
return 'a';
}
if(getA() === 'a'){
console.log('Success');
}
You can make global variable, call it with the function and return the value and call it inside the if condition.
I'm sorry if this is a horrible question, I'm a beginner to JS.
So what I'm trying to do is have a function that runs right away and then that function will run other functions if a certain thing is true.
But, when I run it, it says that the function is not yet defined and I can't figure out how to solve this problem. I thank you for your time.
function start() {
var start = prompt('Yes or no?');
if (start === 'Yes') {
yes();
}
but because its in the beginning the functions it runs are below it and are not able to run.
//declare a function called yes
var yes = function(){
alert('You said yes');
};
function start() {
var start = prompt('Yes or no?');
if (start === 'Yes') {
yes();
}
}
start(); //call first function
Passing function as a parameter example.
var fun1 = function(callback) {
var res = prompt('Your ans please');
callback(res);//pass response to function.
};
var handleRes = function(res){
if (res==='yes'){
alert('you said yes');
}
else if(res==='no'){
alert('you said no');
}
else {
alert('you did not say yes or no');
}
};
//call fun1 pass handleRes function to it for callback
fun1(handleRes);
Order in which you write your function matters in JavaScript in some scenarios. Probably the function you are calling is written below it.
I have written a form validation using JS which ends with return(true);
function check() {
....validation code
return(true);
}
All I want is, need to check if check() function returns true, I want to execute another function.
Code I have tried is as follows:
if(check() === true) {
function() {
//Another function code
}
}
You should use return true; and your if statement doesn't need the === true comparison.
function check() {
//validation code
return true;
}
if(check()) {
//Another function code
}
JSFIDDLE
First of all, return is not a function, you can just do this:
return true;
Now, to only execute myFunction if check returns true, you can do this:
check() && myFunction()
This is shorthand for:
if(check()){
myFunction();
}
You don't need to compare the return value of check with true. It's already an boolean.
Now, instead of myFunction(), you can have any JavaScript code in that if statement. If you actually want to use, for example, myFunction, you have to make sure you've defined it somewhere, first:
function myFunction() {
// Do stuff...
}
You just need to modify your first code snippet. return is a keyword, what you are trying to do is to execute it as a function.
function check() {
....validation code
return true;
}
You'll need to change your 2nd snippet slightly, to execute the function too however... The simplest way is to wrap it as an anonymous function using curly braces:
if(check()) {
(function() {
//Another function code
})();
}
You're not calling the function in your affirmative clause, only declaring it. To call an anonymous function do this:
(function (){...})()
You could type
$this.myFunction=function(){
//code here
}
and to execute some code if a the myFunction function is true, you could use booleans
such as e.g.
if(//your function is true){
and so on
var a = [3,5,2,6,8];
var i = 0;
function x(a[i]) { //This line errors out, why?
}
Please explain why above line errors out? and I am trying to print array elements in reverse order using recursion. If you can write the code also, would be fantastic!
you pass value to function definition. you have to give it when you call function, not when defining
function. you can pass a value when you call a function.
var a = [3,5,2,6,8];
var i = 0;
function x(t) {
}
x(a[i]);
You are defining a function with an argument incorrectly. You give a name to your function arguments, just like any other variable:
function x(array) {
//Do something with your array
}
And then you call the function:
var a = [1,2,3,4,5];
x(a);
As you can read on other answers, you are sort of mixing function declaration and function evaluation (call).
This declares the function x that accepts an a parameter:
function x(a) {
}
And this calls a function x with argument a:
var a = [3,5,2,6,8];
x(a);
or directly:
x([3,5,2,6,8]);
Regarding to the recursive function to reverse an array, this could be an option:
var a = [3,5,2,6,8];
function x(a) {
if (a.length === 0) {
return [];
}
return a.slice(-1).concat(x(a.slice(0, -1)));
}
Using recursion:
var a = [3,5,2,6,8];
function x(p, len) {
if(len >= 0){
alert(p[len-1]);
x(p, len-1);
}
}
// and call
x(a, 5);
DEMO