javascript - ES3 的 Javascript getter/setter
问题描述
我有以下功能,我正在尝试在 Photoshop 中实现(使用 Javascript ES3 编写脚本)。我怎么能把它写成与 ES3 兼容?
function VParabola(s){
this.cEvent = null;
this.parent = null;
this._left = null;
this._right = null;
this.site = s;
this.isLeaf = (this.site != null);
}
VParabola.prototype = {
get left(){
return this._left;
},
get right(){
return this._right;
},
set left(p){
this._left = p;
p.parent = this;
},
set right(p){
this._right = p;
p.parent = this;
}
};
解决方案
您可以Object.defineProperty
在构造函数中使用,例如
function VParabola(s){
this.cEvent = null;
this.parent = null;
var left = null;
var right = null;
this.site = s;
this.isLeaf = (this.site != null);
Object.defineProperty(this, 'right', {
get: function () {
return right;
},
set: function (value) {
right = value;
}
})
Object.defineProperty(this, 'left', {
get: function () {
return left;
},
set: function (value) {
left = value;
}
})
}
推荐阅读
- python - 将数据框列从字符串转换为日期为过去 2262 年的列
- kubernetes - 安装后正在重新创建 Istio ingress pod
- c - 难以理解指向指针增量值的指针
- python - 为什么 python pickle 对 unix 不起作用?
- python - 如何在 matplotlib 中编辑图例的文本颜色?
- vue.js - AWS Cloudfront 托管网站中的 Vue.js 路由
- java - Mac 上的 Java Xattjr - 无法加载资源:libxattrj.dylib
- vue.js - 订购 vue.js 文件的最佳实践
- c# - 如何正确等待来自无模式窗口的输入
- php - 在 PHP 中将布尔表达式解析为 MySql 查询