Object function returns function instead of value

Henk

I have a really rookie question....

If I define a JS object, and I use a function to generate a property value, why does the property value return a function instead of the generated value?

Example:

var object = {
  bla: 1,
  days: [],
  test : function(){
    return 'bla';
  }
}

console.log(object.test);

I would expect object.test to be 'bla'. Instead it's function(){ return 'bla'; }... Why?

Tommaso Bertoni

you have to execute that function, in this way: console.log(object.test());
or, as pointed out by @YuriiKovalenko, you can write the function like this:

var object = {
  bla: 1,
  days: [],
  test : (function(){ return 'bla'; })()
}

and then console.log(object.test); will give you "bla"

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Python recursive function returns None instead of value

분류에서Dev

How to generically type a function that returns a value array of an object array?

분류에서Dev

data-bind returns function instead of text

분류에서Dev

Javascript variable that always returns value of function

분류에서Dev

Modular exponentiation function returns the wrong int value

분류에서Dev

Javascript shuffle function returns same value in for loop

분류에서Dev

Setting an empty list of type object to a function that returns IEnumerable of type object

분류에서Dev

Using c++ 20 Concept to check if a function returns a const value

분류에서Dev

How to create public calculation function that returns calculated value?

분류에서Dev

When i run cron for a database function it returns me the same value

분류에서Dev

Function returns lots of notices

분류에서Dev

Function in Matlab that returns indices

분류에서Dev

JavaScript Object instanceof Function and Function instanceof Object

분류에서Dev

SSRS Lookup Function Returns #Error

분류에서Dev

How to type a function that returns the single element in an array that lives in a property from an object

분류에서Dev

C++ Function supposed to return Long, returning Integer like value instead

분류에서Dev

why I'm getting Not-A-Number instead of a Value returned by my function

분류에서Dev

Function declaration in variable object

분류에서Dev

Pass object through function

분류에서Dev

Sprite collide with object function?

분류에서Dev

TypeError: object is not a function - jquery

분류에서Dev

function to exclude array object

분류에서Dev

Kotlin function as map value

분류에서Dev

PHP function not returning value

분류에서Dev

Override a custom function value

분류에서Dev

Function Pointers - Initial Value

분류에서Dev

APL return value of a function

분류에서Dev

called object is not a function or function pointer in use of ternary

분류에서Dev

Passing a callback function in options object to other function

Related 관련 기사

  1. 1

    Python recursive function returns None instead of value

  2. 2

    How to generically type a function that returns a value array of an object array?

  3. 3

    data-bind returns function instead of text

  4. 4

    Javascript variable that always returns value of function

  5. 5

    Modular exponentiation function returns the wrong int value

  6. 6

    Javascript shuffle function returns same value in for loop

  7. 7

    Setting an empty list of type object to a function that returns IEnumerable of type object

  8. 8

    Using c++ 20 Concept to check if a function returns a const value

  9. 9

    How to create public calculation function that returns calculated value?

  10. 10

    When i run cron for a database function it returns me the same value

  11. 11

    Function returns lots of notices

  12. 12

    Function in Matlab that returns indices

  13. 13

    JavaScript Object instanceof Function and Function instanceof Object

  14. 14

    SSRS Lookup Function Returns #Error

  15. 15

    How to type a function that returns the single element in an array that lives in a property from an object

  16. 16

    C++ Function supposed to return Long, returning Integer like value instead

  17. 17

    why I'm getting Not-A-Number instead of a Value returned by my function

  18. 18

    Function declaration in variable object

  19. 19

    Pass object through function

  20. 20

    Sprite collide with object function?

  21. 21

    TypeError: object is not a function - jquery

  22. 22

    function to exclude array object

  23. 23

    Kotlin function as map value

  24. 24

    PHP function not returning value

  25. 25

    Override a custom function value

  26. 26

    Function Pointers - Initial Value

  27. 27

    APL return value of a function

  28. 28

    called object is not a function or function pointer in use of ternary

  29. 29

    Passing a callback function in options object to other function

뜨겁다태그

보관