admin管理员组文章数量:1389777
I am passing a function in a class or var object as an argument to another function. The function that takes in the function from the class executes that function. It would work fine, however the function of the class calls another function from the class. The console outputs the error that the function being called in the class function is undefined.
The following might illustrate a bit better
//the class variable in someClass.js
function(params...){
getSomethingInClass: function(){
// return some variable
}
functionThatIsPassed: function(arg){
var theCalledFunction = getSomethingInClass();
//do something with theCalledFunction
}
}
//SOME WHERE ELSE in another function in another file
OtherFunction: function(){
//someClass is a variable being used here
FunctionThatTakesFunction(this.someClassVar.functionThatIsPassed);
}
//FunctionThatTakesFunction is implemented in another file
FunctionThatTakesFunction(callbackFun){
callbackFun(someArg);
}
The above will work if I change it to pass the entire object someClass object. That is bad programming practice to pass the object because FunctionThatTakesFunction needs to know the functions of its argument For example
//THIS WORKS!
//other stuff is same
//SOME WHERE ELSE in another function in another file
OtherFunction: function(){
//someClass is a variable being used here
FunctionThatTakesFunction(this.someClassVar);
}
//FunctionThatTakesFunction is implemented in another file
FunctionThatTakesFunction(object){
object.functionThatIsPassed(someArg);
}
I am passing a function in a class or var object as an argument to another function. The function that takes in the function from the class executes that function. It would work fine, however the function of the class calls another function from the class. The console outputs the error that the function being called in the class function is undefined.
The following might illustrate a bit better
//the class variable in someClass.js
function(params...){
getSomethingInClass: function(){
// return some variable
}
functionThatIsPassed: function(arg){
var theCalledFunction = getSomethingInClass();
//do something with theCalledFunction
}
}
//SOME WHERE ELSE in another function in another file
OtherFunction: function(){
//someClass is a variable being used here
FunctionThatTakesFunction(this.someClassVar.functionThatIsPassed);
}
//FunctionThatTakesFunction is implemented in another file
FunctionThatTakesFunction(callbackFun){
callbackFun(someArg);
}
The above will work if I change it to pass the entire object someClass object. That is bad programming practice to pass the object because FunctionThatTakesFunction needs to know the functions of its argument For example
//THIS WORKS!
//other stuff is same
//SOME WHERE ELSE in another function in another file
OtherFunction: function(){
//someClass is a variable being used here
FunctionThatTakesFunction(this.someClassVar);
}
//FunctionThatTakesFunction is implemented in another file
FunctionThatTakesFunction(object){
object.functionThatIsPassed(someArg);
}
Share
Improve this question
asked Jul 31, 2013 at 20:10
SakibSakib
1,5436 gold badges27 silver badges43 bronze badges
3
- 1 And what is your question? – Jackson Ray Hamilton Commented Jul 31, 2013 at 20:13
- I want to just pass the function from the class and call it instead of passing the class object and calling the function after that – Sakib Commented Jul 31, 2013 at 20:26
- FunctionThatTakesFunction(this.someClassVar.bind(this)); – dandavis Commented Jul 31, 2013 at 21:09
2 Answers
Reset to default 2Here are some examples of passing a function into another function: (Fiddle here: http://jsfiddle/FvyUQ/4/)
function Cat() {
this.myMeow = 'Mrrow';
this.scratch = function() {
console.log('Scritchey-scratch.');
}
}
Cat.prototype.meow = function() {
console.log(this.myMeow);
}
Cat.prototype.jump = function() {
console.log('The cat jumped and said ' + this.myMeow + '!');
}
function test(fn) {
fn();
}
function callPrototype(fn, context) {
fn.call(context);
}
var myCat = new Cat();
test(myCat.scratch);
test(myCat.meow);
test(myCat.jump);
test(Cat.prototype.jump);
callPrototype(Cat.prototype.jump, myCat);
I use Closures:
class Sample() {
constructor() {
this.id = 'Sample';
}
method(text) {
console.log(this.id + text)
}
getMethod() {
const ref = this;
return function(text) {
ref.method(text);
}
}
}
Elsewhere:
someFunc() {
const sample = new Sample();
useFunc(sample.getMethod());
}
useFunc(func) {
func('HI');
}
Output: SampleHI
版权声明:本文标题:javascript passing a function in a class that needs another function from that class object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744635746a2616812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论