在函数对象内分配函数而不调用函数本身

编程入门 行业动态 更新时间:2024-10-28 06:30:59
本文介绍了在函数对象内分配函数而不调用函数本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

例如,

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

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

更多推荐

在函数对象内分配函数而不调用函数本身

本文发布于:2023-11-28 09:20:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641822.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   而不   分配   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!