EDIT
Here is an attempt to make my question simpler.
return this.someFunc(); == return { XXX:this.someFunc() };
What do I have to put in for XXX to make this statement true?
test = function(){
this.someFunc = function(){
this.retest = function(){
//...code
}
//...code
}
this.someFunc2 = function(){
//...code
}
return this.someFunc();
}
This function allows me to chain: test().retest();
But what I want to do is return more than one item.
test = function(){
this.someFunc = function(){
this.retest = function(){
//...code
}
//...code
}
this.someFunc2 = function(){
//...code
}
return { XXX:this.someFunc(), //What do I put for XXX
next:this };
}
I want to do this to access another function that test() offers: test().next.someFunc2();
So my problem is this:
I still want to be able to chain like this: test().retest();
But I have to do it like this: test().XXX.retest();
In my code, what is the name that I can put instead of XXX to accomplish this? And is this even possible? I have tried 0
and default
already. Thanks for the help.
You can make a chainable functions like this:
var test = function(){
var self = {};
console.log('test called')
function someFunc() {
console.log('someFunc')
return self;
}
function someOtherFunc() {
console.log('someOtherFunc')
return self;
}
self.someFunc = someFunc;
self.someOtherFunc = someOtherFunc;
return self;
}
test().someFunc().someOtherFunc();
Hope this helps
function test() {
// when test() is called as a regular function
// it's `this` will be `undefined` (in strict mode) or will refer to a global object
// when test() is called with `new` operator
// it's `this` will be an instance of `test` object
if(!(this instanceof test)) {
// create a new object
return new test();
}
else {
// this is a `new object` creation
console.log('new Test created');
}
}
// declare functions which will be accessible on test object
test.prototype.someFunc = function() {
console.log('someFunc');
return this;
};
test.prototype.someFunc2 = function() {
console.log('someFunc2');
return this;
};
// declare a `chaining` function - will create a new `test` object
test.prototype.test = function() {
return new test(); // new instance will be created
};
With this code you are now able to run the following code
test().someFunc().someFunc2().test().someFunc().someFunc().test().test()
The following string will be written at the console
new Test created
someFunc
someFunc2
new Test created
someFunc
someFunc
new Test created
new Test created
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With