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!

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.

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

1 Comment

Thanks for the help! That seems so obvious now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.