首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript的call()和apply()函数在Coldfusion中的等价物

JavaScript的call()和apply()函数在Coldfusion中的等价物
EN

Stack Overflow用户
提问于 2012-02-02 12:37:15
回答 3查看 427关注 0票数 0

我需要实现类似于Coldfusion 8中JavaScript的call()或apply()函数的功能。我需要一种方法来为被调用的函数动态绑定“this”上下文。除了手动传入参数列表中的上下文之外,还有其他方法可以做到这一点吗?不幸的是,我很难在谷歌上搜索线索,因为我似乎无法搜索关键字“this”。

代码语言:javascript
复制
<!--- component A --->
<cfcomponent>
    <cffunction name="init">
        <cfset this.value = "My name is A">
        <cfreturn this>
    </cffunction>

    <cffunction name="setDelegate">
        <cfargument name="delegate">

        <cfset this.delegate = delegate>
    </cffunction>

    <cffunction name="runDelegate">
        <cfoutput>#this.delegate()#</cfoutput>
    </cffunction>
</cfcomponent>

<!--- component B --->
<cfcomponent>
    <cffunction name="init">
        <cfset this.value = "Hello, I am B">
        <cfreturn this>
    </cffunction>

    <cffunction name="call">
        <cfoutput>#this.value#</cfoutput>
    </cffunction>
</cfcomponent>

<!--- main cfm --->
<cfset mrA = createObject('component', 'A').init()>
<cfset mrB = createObject('component', 'B').init()>

<cfset mrA.setDelegate(mrB.call)>

<!--- I want to return "Hello, I am B", --->
<!--- but it's going to output "My name is A" in this case --->
<cfoutput>#mrA.runDelegate()#</cfoutput>

在上面的例子中,“this”上下文属于A,但我希望将上下文绑定到B以利用B的属性。

在JavaScript中很容易做到这一点,只需将mrB传入call()函数: mrA.runDelegate.call( mrB );该函数会将' This‘上下文设置为mrB而不是mrA。

EN

回答 3

Stack Overflow用户

发布于 2012-02-03 01:28:58

假设您正在尝试从给定组件中动态调用一个方法,您可能需要执行以下操作

代码语言:javascript
复制
<cfinvoke component="#this#" method="#methodToCall#">
   <cfinvokeargument name="#prop#" value="#someValue#" />
</cfinvoke>

这将使用整个"this“并调用组件中的方法,因此您的上下文应该是完整的。

如果你只是以标准的方式调用组件中的一个方法,那么"this“是可用的,不需要做任何特殊的事情。

为了给你一个更好的解决方案,我们需要知道你想要实现的是什么。

票数 1
EN

Stack Overflow用户

发布于 2012-02-02 23:56:32

我并不认为这是可能的,因为这个作用域是一个CFC实例的公共作用域,替换这个作用域的上下文并不是一件简单的事情。然而,正如@rip747建议的那样,如果你更清楚地知道你想要什么,也许有一种方法可以做到这一点。

票数 0
EN

Stack Overflow用户

发布于 2012-02-03 10:08:16

对不起,我想我已经想通了。我还应该将上下文与要调用的委托一起传递。当我在JavaScript中思考如何做到这一点时,我突然意识到我遗漏了一个论点。

代码语言:javascript
复制
<!--- Component A --->
<cfcomponent>
    <cffunction name="init"> 
        <cfset this.value = "My name is A">
        <cfreturn this>
    </cffunction>

    <cffunction name="setDelegate">
        <cfargument name="delegate">
        <cfargument name="context">

        <cfset this.delegate = delegate>
        <cfset this.context = context>
    </cffunction>

    <cffunction name="runDelegate">
        <cfoutput>#this.delegate()#</cfoutput>
    </cffunction>
</cfcomponent>

<!--- component B --->
<cfcomponent>
    <cffunction name="init">
        <cfset this.value = "Hello, I am B">
        <cfreturn this>
    </cffunction>

    <cffunction name="call">
        <cfoutput>#this.value#</cfoutput>
    </cffunction>
</cfcomponent>

<!--- main cfm --->
<cfset mrA = createObject('component', 'A').init()>
<cfset mrB = createObject('component', 'B').init()>

<cfset mrA.setDelegate("call", mrB)>

<cfoutput>
    <cfinvoke component="#mrA.context#" method="#mrA.delegate#"></cfinvoke>
</cfoutput>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9107235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档