在函数对象内分配函数而不调用函数本身 [英] Assigning function within function object without invoking the function itself

查看:110
本文介绍了在函数对象内分配函数而不调用函数本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在函数对象属性中分配一个函数,而不实际调用函数本身。

例如,

我有以下的函数对象类定义

  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();

这篇关于在函数对象内分配函数而不调用函数本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆