I write this:

$scope.localSearch = function () { var matches = 123; return matches; }; console.log($scope.localSearch); 

In the console:

enter image description here

How to return the variable itself and not the function?

  • not very familiar with javascript, but probably: console.log ($ scope.localSearch ()); - Alexander
  • one
    for example, call it console.log($scope.localSearch()); - Grundy
  • @Grundy, tin)) in the lessons they learned that if the function returns values, brackets are not needed) thanks - Sdafs Fasafs
  • five
    @SdafsFasafs, show this place that if the function returns values, no parentheses are needed - Grundy
  • $scope.localSearch - contains a link to the function with the body `var matches = 123; return matches; , что - бы вернуть значение нужно функцию запустить () ` - pepel_xD

2 answers 2

Or so

 $scope.localSearch = function () { var matches = 123; return matches; }(); console.log($scope.localSearch); 
  • Lokanichnye! How much does the behavior of the function affect the addition () at the end? It seems to be done at once, but how will it continue to behave? Now you can call everywhere without brackets $scope.localSearch . What is the other side of the coin? - borodatych
  • @borodatych, I didn’t understand the question at all :) we just immediately performed the function and wrote the result into a variable. In principle, everything. - Grundy
  • Maybe this: a minus of the approach is that due to the fact that execution occurs immediately, the necessary data must be declared before the function is declared. What means, what is lost ?? - borodatych
  • @borodatych, this is if the function uses some left-handed data, and it’s not clear what the binding is - Grundy
  • I added to myself under the UPD about what I'm saying, please comment, I am suddenly mistaken. - borodatych

It is possible so:

 $scope.localSearch = function () { var matches = 123; return matches; }; $scope.myVar = $scope.localSearch(); console.log( $scope.myVar ); 

UPD
Use via function(){}() :

 $scope.test = 0; $scope.localSearch = function () { return $scope.test; }(); $interval(function(){ console.log("EVERY 1 SEC: " + $scope.localSearch); },1000); $interval(function(){ $scope.test++; console.log("EVERY 3 SEC: " + $scope.test); console.log("EVERY 3 SEC: " + $scope.localSearch); },3000); 

If localSearch should depend on the $scope.test and primitive $scope.test , then it is better to abandon this method!

  • yes, with a primitive value — yes, but if $scope.test would be an object and its fields changed — then there would be no differenceGrundy
  • Indeed, thank you. Corrected. But something gnaws at me anyway .... - borodatych