0

How can i do this:

var obj = {x: 2, y: 4};
obj(); //return function
obj.x; //return 2

like jquery:

$.fn
$()
4
  • Could you provide some more detail? I'm not clear what obj() is supposed to do, or what it is supposed to return exactly. Commented Jan 28, 2018 at 5:08
  • obj(); //return function ? Commented Jan 28, 2018 at 5:08
  • See stackoverflow.com/questions/48479510/… Commented Jan 28, 2018 at 5:09
  • var func = function(arg1){console.log(arg1)}; Commented Jan 28, 2018 at 5:10

1 Answer 1

2

You can assign properties to functions since functions are objects:

var obj = function ( ) {
    console.log( 'Called' );
};
obj.x = 2;
obj();
console.log( obj.x );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. but it calls both together. is it possible if i call obj.x it doesn't call obj() ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.