Reputation: 615
I am trying to build two functions to work like jQuery functions work, for example: jQuery( 'select').val();
This works:
function func(a) {
console.log(1);
return a;
}
func.sub = function(n) {
console.log(2);
}
func.sub(2);
But this doesn't:
function func(a) {
console.log(1);
return a;
}
func.sub = function(n) {
console.log(2);
//return func result[n];
}
func([1,2,3]).sub(2);
How can I make this second code work and read func() result on sub()?
Upvotes: 1
Views: 42
Reputation: 198436
You need to have func
return an object that has a method sub
.
function Subbable(x) {
this.value = x;
}
Subbable.prototype.sub = function(b) {
return this.value - b;
}
function func(a) {
return new Subbable(a);
}
func(10).sub(2)
// 8
jQuery's $(...)
typically returns a jQuery object that contains a collection of the selected nodes, and whose prototype has all the nice goodies like .attr
and .css
.
Upvotes: 1