0


I was thinking about this... I know that you can do Object.prototype.example() but I was wondering how to do something like this: ExampleFunction().prototype.exampleHandler. Sort of like how this works:document.querySelector().id I think that it is possible. If you have any questions about it, please comment!

Thanks for helping out!

Bergi
671k162 gold badges1k silver badges1.5k bronze badges
asked May 17, 2015 at 17:43
1
  • What exactly does this have to do with prototypes? Do you think .prototype is special somehow? Commented May 17, 2015 at 21:06

1 Answer 1

1

To make something work like document.querySelector().id you just need to add a property to a function, and as functions are objects it's easy, or return an object with those properties when the function is called, like this

var example = function(what) {
 return {
 id : what
 }
}
var foo = example('foo').id; // TADA
example.bar = function() {
 return {
 id : 'not foo'
 }
}
var bar = example.bar().id;
document.body.innerHTML = foo + ' - ' + bar;

And that's how chaining works in it's simplest form.

answered May 17, 2015 at 17:48
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help! That seems so obvious now.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.