Reputation: 519
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!
Upvotes: 0
Views: 39
Reputation: 318342
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.
Upvotes: 1