Declare variable value in function call

Asked

Viewed 28 times

0

I don’t know how to call this to Google search or if it is possible to do this. I have an object that is as follows:

var clear = {
    Search_r: false,
    Search_f: false,
    Search_e: false
}

And I have the following function

function clearSearch(clear) {
    if(clear.Search_r) {
        $(".search-r").html("")
    }
    if(clear.Search_f) {
        $(".search-f").html("")
    }
    if(clear.Search_e) {
        $(".search-e").html("")
    }
}

I call it that

    clear.Search_r = true
    clearSearch(clear)

It works very well, but I wanted to know how to pass the true within the function call and not on the top line. Something like:

clearSearch(clear.Search_r = true) //isso não funciona e nem da erro

I mean, it doesn’t work, from Undefined. How would be correct and if possible, how to write this in Google to find other solutions?

  • What is your intention to do this (pass the value to variable within the call)? being that the first way is working.

  • 1

    "i have a variable of type array which is the following", your example is not an array, is an object, it would be an array if so, for example: var clear = [ { ...}, {....} ];

  • @sant0will use 1 line instead of two and learn how to do this for something more complex

  • @Ricardopunctual legal, thank you

  • clearSearch(clear.Search_r = true) this syntax works in some languages, but unfortunately in javascript no :( you have to pass the object with a boolean property inside (eg: var parm = { Search_r = true }; clearSearch(param), or in the simplest way: clearSearch(true)

  • @Ricardopunctual is that I really wanted to use the fewest possible lines, in this example would still use 2

Show 1 more comment

2 answers

3

Since you are using only one parameter of the Object, it is not necessary to pass it completely, just pass true or false for the function, for example:

function clearSearch(clear = true) {
  if (clear) {
    console.log("Limpa o console")
  } else {
    console.log("Ops")
  }
}

clearSearch(true)
clearSearch(false)
clearSearch()

If it is necessary to pass the Object, instead of doing funcao(obj.clear = true), just use

function clearSearch(obj = {field1: true, field2: false}) {
    if(obj.field1) {
        console.log("Limpa o campo 1")
    } else {
      console.log("Ops 1")
    }
    
    if(obj.field2) {
        console.log("Limpa o campo 2")
    } else {
      console.log("Ops 2")
    }
}

clearSearch({field1:true,field2:true})
clearSearch({field2:false,field2:false})

clearSearch({field1:true,field2:false})
clearSearch({field2:false,field2:true})

clearSearch()

In addition to the above options, you can also use the function Object.assign. With this function you can set the default values of the object.

function clearSearch(obj) {
    obj = Object.assign({clear: true}, obj)

    if(obj.clear) {
        console.log("Limpa o console")
    } else {
      console.log("Ops")
    }
}

clearSearch({clear:true})
clearSearch({clear:false})

clearSearch()
clearSearch(false)

Note: The value you are trying to pass is an Object and not an Array

  • i want to use in various options of if, search_r, search_f

  • @flourigh Then you can use as per the second example. As your sample code is simple, I based on it.

  • very good, loved the function, I will greatly improve mine with the help of yours, that would be object and parameter pass in function call to search n google to improve?

  • @flourigh There is no specific term for this, in my examples I used "default values"

  • I got it, but it was really cool, I’ll implement it in mine, I really appreciate it

  • I liked the option 3 that is to assign, it would work as in 2? with more than one "Field"?

  • 1

    @flourigh is possible to use as many fields as you want in both cases.

Show 2 more comments

2


The way your function is written, the parameter passage you want is given this way:

clearSearch({ Search_r: true })
  • perfection, worked incredibly, in this case is pass parameter in function call? and only works because I have an object?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.