【问题标题】:How are these two components different?这两个组件有何不同?
【发布时间】:2023-04-02 09:24:01
【问题描述】:

其中一个比另一个更好吗?有什么不同?它们似乎可以互换

component
{
    property name="some_thing" type="string" value="";
}

component
{
    this.some_thing = "";
}

【问题讨论】:

  • this 作用域本质上是公共的,所以它可以被组件之外的东西修改,property 不能。它的范围和访问受到更多限制。另外,属性还有更多功能helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/…
  • 哦,这很有道理。我很高兴你指给我看医生。因为没想到​​查cf标签版本,所以没找到。
  • 是的,您经常不得不求助于它,因为 cfscript 文档......更好......但没有达到 cfml 文档的水平。属性的一个很好的特性是使用它们来自动生成 getter 和 setter。

标签: coldfusion coldfusion-10 cfml


【解决方案1】:

cf 属性

在 CF8 之后,'cfproperty' 允许设置一个隐式 setter/getter。

它还用于创建 Web 服务和 ORM 应用程序,并具有大量配置属性:

https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-p-q/cfproperty.html

Setter/getter

com/foo.cfc

component accessors='true' { 

    // set properties & variables above any component methods

    property name='bar' type='string';
    this.setBar('foo');

    function init(){
        return this;
    }

}

在模板“foo.cfm”内:

foo = new com.foo();
WriteDump(var=foo.getBar());
// foo

'this'范围

“this”范围可以在组件内部和外部访问。

com/foo.cfc

component { 

    // set properties & variables above any component methods

    this.bar = 'foo';

    function init(){
        return this;
    }

}

在模板“foo.cfm”内:

foo = new com.foo();
WriteDump(var=foo.bar);
// foo

组件内的“变量”范围

组件内的变量范围不能从组件外访问。

【讨论】:

    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 2019-09-09
    • 2021-10-06
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多