推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
cutemurphy2888

一个 setter 死循环错误·

  •  
  •   cutemurphy2888 · Mar 24, 2022 · 4421 views
    This topic created in 1684 days ago, the information mentioned may be changed or developed.
    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 replies  •  2022-05-16 18:30:14 +08:00
    10bkill1p
    10bkill1p
    Mar 24, 2022
    1
    用一个中间变量去传,直接修改是会死循环的。
    ccyu220
    ccyu220
    Mar 24, 2022
    2
    确实第一次见 get set 和参数名写一样的...
    faceRollingKB
    faceRollingKB
    Mar 24, 2022
    3
    class Dep {
    constructor(value) {
    this._value = value;
    }

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

    set value(newValue) {
    this._value = newValue;
    }
    }
    lisongeee
    lisongeee
    Mar 24, 2022
    4
    js/python 不像 kotlin ,setter/getter 需要手动声明额外的变量保存状态,kotlin setter/getter 内部有一个关键字 field ,无需手动额外声明变量
    cutemurphy2888
    cutemurphy2888
    OP
    Mar 24, 2022
    5
    @lisongeee 那为什么设计者不借鉴一把 /
    lisongeee
    lisongeee
    Mar 24, 2022❤️2
    6
    我不到啊
    KMpAn8Obw1QhPoEP
    KMpAn8Obw1QhPoEP
    Mar 24, 2022 via Android
    7
    哈哈哈楼上对话太逗了
    misdake
    misdake
    Mar 24, 2022
    8
    变量数据总要保存到一个地方,比如_value 这种。setter 里设置到同一个 setter 上肯定不行呀。
    AV1
    AV1
    Mar 24, 2022
    9
    @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
    TWorldIsNButThis
    Mar 24, 2022 via iPhone
    10
    @cutemurphy2888 kotlin 没有 java 或者 js 里的字段,
    只有 property 的概念,property 是否对应一个真正的字段是编译器判断
    EyeLip
    EyeLip
    Mar 24, 2022
    11
    @lisongeee 刀酱?
    blu10ph
    blu10ph
    Mar 24, 2022
    12
    @cutemurphy2888 我刚才提的需求,上午为什么没有实现?~
    magewu1223ll
    magewu1223ll
    Mar 24, 2022
    13
    应该是 get 引起的吧,每次都读取 一读取就触发 get 然后 get 里面又有个读取,然后触发 get 。。。。。。
    thinkershare
    thinkershare
    Mar 24, 2022
    14
    因为它本来就应该死循环, 设计上这样写就应该死循环!
    jadehare
    jadehare
    Mar 24, 2022
    15
    get value 死循环了,this.value = get value(),相当于方法自己调用自己了
    lisongeee
    lisongeee
    Mar 24, 2022
    16
    @EyeLip 我是虎弟,你,来沈阳,指定没你好果汁吃
    volCan0
    volCan0
    Mar 24, 2022
    17
    看看 vue3 的 ref
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4377 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 01:10 · PVG 09:10 · LAX 17:10 · JFK 20:10
    ♥ Do have faith in what you're doing.