- 分别使用ts和js对字符串属性text封装get set 方法,代码如下
test.ts
const {ccclass, property} = cc._decorator;
@ccclass
class NewClTetass extends cc.Component {
@property({
default: "hello",
})
public hideText: string = "hello";
@property({
get: () => {return this.hideText},
set: (value : string) => { this.hideText = value; }
})
public text: string = "hello";
public onLoad() {
}
}
test-in-js.js
cc.Class({
extends: cc.Component,
properties: {
hideText:"hello",
text:{
get:function(){
return this.hideText;
},
set:function(value){
this.hideText = value;
}
}
},
onLoad: function () {
},
});
回到编辑器,将test.ts和test-in-js.js挂载到canvas节点,效果如图
此时,分别修改test.js和test-in-js.js的text属性为hello again,效果如图
test-in-js能够正常地同步至hideText。
而test.ts并不能同步至hideText。
另外在test.ts中 将hideText修改为_hideText,哪怕hideText的访问权限是public 在编辑器中也不会显示这个属性,我有点怀疑人生。。这个public形同虚设。。
@Jare老师 您的40寸液晶超清大锅已经到楼下了,麻烦取一下快递233。