EdwardZZZ / articles

工作点滴记录
2 stars 0 forks source link

setter getter #24

Open EdwardZZZ opened 7 years ago

EdwardZZZ commented 7 years ago

与java中的setter 和 getter方法大不同

public class A{
    private String name;

    class A(name){
        this.name = name;
    }

    public void setName(name){
        this.name = name;
    }

    public String getName(){
        return this.name;
    }
}

A a = new A();
a.getName();
class A {
    constructor(name){
        this.setName = name;
    }

    set setName(name){
        console.log('set name', name);
        this.name = name;
    }

    get getName(){
        console.log('get')
        return this.name;
    }
}

let a = new A(123);

console.log(a.getName)

a.setName = 456;

console.log(a.getName)

Object.defineProperty(window, '____', {
    set(...props) {
        console.log(this, props);
    }
});
EdwardZZZ commented 4 years ago
const obj = {
    n: 111,
    get a() {
        return function(...props) {
            console.log('a, ', this.n, ...props);
        }
    },
    get b() {
        return (...props) => {
            console.log('b, ', this.n, ...props);
        }
    },
    c(...props) {
        console.log('c, ', this.n, ...props);
    }
}

const { a, b, c } = obj;

a.bind({ n: 222 })(123, 456)
b.bind({ n: 333 })(123, 456)
c.bind({ n: 444 })(123, 456)