V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
cutemurphy2888
V2EX  ›  JavaScript

一个 setter 死循环错误·

  •  
  •   cutemurphy2888 · 2022-03-24 10:58:09 +08:00 · 2857 次点击
    这是一个创建于 735 天前的主题,其中的信息可能已经有所发展或是发生改变。
    class Dep {
    constructor(value) {
    this.value = value;
    }

    get value() {
    return this.value;
    }

    set value(newValue) {
    this.value = newValue;
    }
    }

    let dep = new Dep("cc");
    dep.value='dd';
    18 条回复    2022-05-16 18:30:14 +08:00
    10bkill1p
        1
    10bkill1p  
       2022-03-24 11:17:27 +08:00
    用一个中间变量去传,直接修改是会死循环的。
    ccyu220
        2
    ccyu220  
       2022-03-24 11:27:27 +08:00
    确实第一次见 get set 和参数名写一样的...
    faceRollingKB
        3
    faceRollingKB  
       2022-03-24 11:48:15 +08:00
    class Dep {
    constructor(value) {
    this._value = value;
    }

    _value
    get value() {
    return this._value;
    }

    set value(newValue) {
    this._value = newValue;
    }
    }
    lisongeee
        4
    lisongeee  
       2022-03-24 11:50:21 +08:00
    js/python 不像 kotlin ,setter/getter 需要手动声明额外的变量保存状态,kotlin setter/getter 内部有一个关键字 field ,无需手动额外声明变量
    cutemurphy2888
        5
    cutemurphy2888  
    OP
       2022-03-24 11:57:45 +08:00
    @lisongeee 那为什么设计者不借鉴一把 /
    lisongeee
        6
    lisongeee  
       2022-03-24 13:37:23 +08:00   ❤️ 2
    我不到啊
    enchilada2020
        7
    enchilada2020  
       2022-03-24 13:41:43 +08:00 via Android
    哈哈哈楼上对话太逗了
    misdake
        8
    misdake  
       2022-03-24 13:45:52 +08:00
    变量数据总要保存到一个地方,比如_value 这种。setter 里设置到同一个 setter 上肯定不行呀。
    DOLLOR
        9
    DOLLOR  
       2022-03-24 14:31:37 +08:00
    @faceRollingKB
    或许用私有变量更好
    class Dep {
    constructor(value) {
    this.#value = value;
    }

    #value
    get value() {
    return this.#value;
    }

    set value(newValue) {
    this.#value = newValue;
    }
    }

    let dep = new Dep("cc");
    dep.value = 'dd';


    @cutemurphy2888
    大概因为没几个人在 JS 里写 class 吧。
    TWorldIsNButThis
        10
    TWorldIsNButThis  
       2022-03-24 14:53:43 +08:00 via iPhone
    @cutemurphy2888 kotlin 没有 java 或者 js 里的字段,
    只有 property 的概念,property 是否对应一个真正的字段是编译器判断
    EyeLip
        11
    EyeLip  
       2022-03-24 16:30:59 +08:00
    @lisongeee 刀酱?
    blu10ph
        12
    blu10ph  
       2022-03-24 16:40:22 +08:00
    @cutemurphy2888 我刚才提的需求,上午为什么没有实现?~
    magewu1223ll
        13
    magewu1223ll  
       2022-03-24 16:44:18 +08:00
    应该是 get 引起的吧,每次都读取 一读取就触发 get 然后 get 里面又有个读取,然后触发 get 。。。。。。
    thinkershare
        14
    thinkershare  
       2022-03-24 16:44:56 +08:00
    因为它本来就应该死循环, 设计上这样写就应该死循环!
    jadehare
        15
    jadehare  
       2022-03-24 16:52:05 +08:00
    get value 死循环了,this.value = get value(),相当于方法自己调用自己了
    lisongeee
        16
    lisongeee  
       2022-03-24 18:41:38 +08:00
    @EyeLip 我是虎弟,你,来沈阳,指定没你好果汁吃
    volCan0
        17
    volCan0  
       2022-03-24 19:30:28 +08:00
    看看 vue3 的 ref
    Opportunity
        18
    Opportunity  
       2022-05-16 18:30:14 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1011 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 848ms · UTC 19:31 · PVG 03:31 · LAX 12:31 · JFK 15:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.