javascript - How to bind a callback function to a variable
高洛峰
高洛峰 2017-06-26 10:53:29
0
3
867

Can Javascript bind a callback function to a variable?
That is: when the value of this variable changes, the callback function is triggered and the content in the callback function is executed.

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
迷茫

var test = {
_age : 0,
methods:function(a)
{

console.log("发生变化了值为:"+a);

},
//_Age reading and writing
set age(age) {

if(age!=this._age)
{
    this.methods(age);
    this._age = age;
}}, 

get age() {return this._age;}
};
You can use the set and get methods of the object to perform the desired results

黄舟

Cannot be implemented directly.
But it can be achieved in other ways.

var obj = {
        set: function (key, value) {
            if(['set', 'change'].indexOf(key) > -1) return;

            this[key] = value;

            this.change();
        },

    };

    obj.change = function(){
        alert(1)
        console.log(this);
    }

    obj.set('name', 'segmentfault');
    
     // 将你需要的变量设为obj的一个属性
     // 更改变量用obj.set()这个方法
淡淡烟草味

js set/get
You can add your logic code in the set method, so that your code will be triggered every time it is modified

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!