6

I know I'm going to get the people who say I simply shouldn't do this, but I am curious how people accomplish it.

I know I've seen where you can type the name of a property and get one value, but then you add the parenthesis to the end and it accesses a method.

Visually, I'm saying this:

foo returns 'bar'
foo() performs a function

The question is how?

4
  • 1
    Well where did you see it? (Maybe you're confusing it with adding properties to functions, which enables one to write fn() and also fn.prop1.) Commented Aug 29, 2015 at 22:51
  • I've been trying to remember. I've seen it a few times over the years, and now that I want to figure it out, I can't remember. :) Commented Aug 29, 2015 at 22:55
  • @doldt, you might be correct. But why would you be able to do that, and not this? Commented Aug 29, 2015 at 23:47
  • This can only be done in the special cases shown in the answers with valueOf() and toString(), it can't be done in a universal way. Commented Aug 30, 2015 at 0:04

3 Answers 3

2

This isn't possible, due to how properties are resolved on objects. This is the only thing that comes remotely close:

function f() {
  return 'I am being returned from the function.';
}

f.toString = function () {
  return 'I am a property on the variable.';
}

console.log('f() says: ' + f());
console.log('f says: ' + f);

It relies on the fact that JS typecasts functions via the .toString method in certain scenarios.

Sign up to request clarification or add additional context in comments.

Comments

0

No, but kind of...

This can kind of be done with valueOf

var foo = function() {
  console.log("From Function");
  return "Function";
};

foo.valueOf = function() {
  return "Variable";
};

alert( 'Output is: ' + foo() )
alert( 'Output is: ' + foo   )

This cannot be done with JavaScript because item in an object can be two different objects.

If you want a property to be a function and an object, that's different

9 Comments

@thecodeparadox anytime, it is casted with a number / string, or really anything it still snap into it's valueOf
@thecodeparadox as vihan specified above, it is possible, due to the nature of JavaScript prototypal inheritance. When you try to convert something into a primitive type, there are several functions calling up, and one of them is valueOf() MDN ValueOf() Reference
@vihan, so a property can be a function and an object, but not a function and a string?
@AdamCook Yes, that's because everything in JavaScript is an object. So you can add a property to anything.
"So you can add a property to anything" - No you can't. Try var x = 1; x.newProp = "something"; and then check the value of x.newProp. Not everything in JavaScript is an object. But all functions are.
|
0

Note, workaround utilizing setTimeout

var foo = (function() {
  var fn = function() {
    return foo && typeof foo === "string" ? function() {
      return "foobar"
    } : "bar";
  };
  setTimeout(function() {
    foo = fn()
  });
  return fn()
}());

console.log(foo); // `"bar"`

setTimeout(function() {
  console.log(foo()); // `"foobar"`
});

Comments

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.