我需要在Coldfusion 8中实现与JavaScript的call()或apply()函数类似的功能。我需要一种方法来动态绑定我的'this'函数被调用。缺少在参数列表中的上下文中手动传递,是否有任何其他方式来执行此操作?不幸的是,我很难在Google上搜索线索,因为我似乎无法搜索关键字“this”。在Coldfusion for JavaScript的调用()和apply()函数中相当于
<!--- 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>
在上面的例子中,“这个”上下文属于A,但我要绑定的上下文到B以利用B的属性。
只需将mrB传入call()函数即可在JavaScript中执行此操作: mrA.runDelegate.call(mrB);这会将'this'上下文设置为mrB而不是mrA。
原谅我,但我没有按照你的问题。如果你举了一个你想要完成的事情的例子,这可能会有所帮助。 – rip747 2012-02-02 14:55:29
我已经添加了一些示例代码来阐明我试图实现的内容 – xess 2012-02-03 01:56:45