Functions inside objects

How can i call a function if that whole function is stored in an object?
A challenge suggested that objects can store functions, i tried calling a function which was inside an object, but didn’t work. So what is the use of it being stored in an object?

Const obj = {
func1: function test(){return 3>5;}

Thank you.
@snowmonkey if you have time for this one.

You would call it as an object property:


A little bit of experimenting goes a long way. A little bit of research also goes a long way.

What did you try to see how this might work?

What did you Google to see how this might work?

Is there anything from your experimenting or research that we can help clarify?


In this case, i think the capital in Const would be a thing. And you’ve created an interesting question i now want to play with.

Your code could be rearranged like this:

function test(){ return 3>5; }

const obj = {
  func1: test

We could define the function separately like this and, as you’ve created the named function, i think both actually create a global called test, then assign the reference obj.func1 to that test function.

Had you simply done

const obj = {
  func1: function(a, b){ return a+b; }

The function is not named, so no variable for it. Only the one reference in our object: obj.func1("I am a ", "String!").

Try, with your version, console.log(window.test) and see what it shows.

1 Like

you declared your object wrong(you used Const with capital letter, instead of const.
Have you really tried to call the function? Im surprised it didnt work, if you followed the general pattern of calling any propery of an object, it should have worked.
PS: functions inside objects are often refered in js as “methods”

I didn’t copy that const from somewhere else, i just typed const and the field of create topic here did the uppercase .
Didn’t pay attention that’s all.
I did try to call it like usual call but didn’t work. I guess somebody up here suggested a way, but thank you.

1 Like

You always give me more then i asked that go in direction of what i asked. Thanx always.
Your answer is bookmarked again.

Fair warning, if you have a psychological condition that prevents you from attempting to use Google, you will struggle to become a software engineer. This is a critical skill for programmers.

This is, unfortunately, literally the definition of writing new software. You are constantly researching stuff.

At least in my experience, the only way to write new code it to look stuff up and experiment.

1 Like

You won’t see methods on object literals all that often. Usually, you will use classes or constructor functions and the new operator.

It’s JS OOP which is a large subject and a bit of a rabbit hole (considering the limited scope of your question).

Well ish -factory functions often return an object literal, as an interface to that particular closure. That returned object is usually composed of functions, either imported or defined within that closure.

You’re right, though, it isn’t common to write an object literal, fully exposed, with some methods defined. Most commonly, you would pass data into something doing the constructing - building a one-time object is not very DRYish.

I consider factory functions under the same umbrella as constructor functions. A function returning an object (implicitly or explicitly) is pretty much a constructor function in my book. Although, semantically they are not the same and you likely won’t call a factory function a constructor function to avoid confusion.

As you said, I was more referring to the top-level object literal with methods on it. That you won’t see very often (maybe in some older code).

1 Like