我试图在函数对象属性中分配一个函数,而不实际调用函数本身。
例如,我有以下的函数对象类定义
function objectOne(name,value,id){ this.name = name; this.value = value; this.id = id; this.methodOne = methodFunction(this); }最后一行 this.methodOne = methodFunction ); 我想将当前对象传递给该函数,但同时我不想立即执行该函数。
但是,如果我这样做没有括号this.methodOne = methodFunction那么这个对象的参数不会作为参数传递给函数。
预先感谢您
解决方案这个页面帮助我解释这个概念是最好的。基本上会做类似下面的事情。
function objectOne(name,value,id){ function methodFunction值){ //做某事} var that = this; this.methodOne = function(){ methodFunction(that); }; } var x = new objectOne(one,2,3); x.methodOne();
I am trying to assign an function within an function object property without actually invoking then function itself.
for instance,
I have the following function object class definition
function objectOne(name, value, id){ this.name=name; this.value=value; this.id=id; this.methodOne=methodFunction(this); }the last line this.methodOne=methodFunction(this); I want to pass the current object to the function but at the same time i don't want to execute the function right now.
But if I do it this way without the bracket this.methodOne=methodFunction then the argument of this object would not be passed as a parameter to the function.
Is there a way to work through this.
Thank you in advance
解决方案This page helped explain this concept to me the best. Essentially would be doing something like the following.
function objectOne(name, value, id) { function methodFunction(value) { // do something } var that = this; this.methodOne = function() { methodFunction(that); }; } var x = new objectOne("one", 2, 3); x.methodOne();
更多推荐
在函数对象内分配函数而不调用函数本身
发布评论