Yes, but your syntax is a bit off. When you create a property that is assigned to the function, you don't add ()
because that will invoke the function.
Later, when you are ready to invoke the function stored in the property, you do use ()
to can call the function as a "method" of the object.
The main point being that in JavaScript, we can refer to functions by just saying their name and we can invoke functions by saying their name followed by parenthesis.
function runFunc(){
console.log("hello");
}
// Create new object and assign the function to a property
obj= { func: runFunc }; // <-- No parenthesis after function name here
obj.func(); // Call function as a "method" of the object
// Or, combine the above and create the property and assign the function at once
// Notice here that the function is anonymous. Adding a name to it won't throw
// an error, but the name is useless since it will be stored under the name of
// the object property anyway.
obj.otherFunc = function(){ console.log("Hello from other function! ") };
obj.otherFunc(); // <-- Here, we use parenthesis because we want to call the function