如何拦截和修改任何对象的特定属性 [英] How to intercept and modify a specific property for any Object

查看:43
本文介绍了如何拦截和修改任何对象的特定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于任何未知数量的对象,如果需要,我想拦截和更改某些属性.我尝试过 getter 和 setter,但我只能实现接近我想要的并且仅适用于已知对象.

For any unknown number of objects, there are certain properties I want to intercept and change if needed. I have tried getters and setters, but I was only able to achieve close to what I wanted and only for known objects.

以下是我想要实现的目标的示例:

The following are examples of what I am trying to achieve:

在我的范围/闭包之外创建的对象

Objects being created outside of my scope/closure

如您所见,这些对象无法从外部访问,我想要做的是检查每个 year_of_birth 属性,并在为任何未知对象创建或更改该属性时对其进行修改.我得到的最接近的是通过 getter/setter,但我必须传递对象,并且每次更改其值时它都会循环自己.

As you can see these are objects that cannot be accessed from outside and what I want to do is check every year_of_birth property and modify it whenever it is created or altered for any unknown object. The closest I got was through getters/setters, but I had to pass the object and it would loop itself every time it change its value.

尝试通过检查当前年份的年龄来纠正 year_of_birth 的示例

An example that tries to correct the year_of_birth by checking the age with the current year

Object.defineProperty(unknown_object, 'year_of_birth', {
    set: function(year) {
        if (this.age && 2017 - this.age > year) {
            this.year_of_birth = 2017 - this.age;
        }
    }
});

Object.defineProperty(unknown_object, 'year_of_birth', {
    get: function(year) {
        if (this.age && 2017 - this.age > year) {
            return 2017 - this.age;
        }
    }
});

但仍然不能很好地工作,并且只适用于我可以直接访问的单个对象.

but still did not work well and only worked with singular objects that I had direct access.

有没有办法做到这一点?请不要使用像 jQuery 这样的库/框架的解决方案.

Is there any way to do this? Please no solutions with libraries/frameworks like jQuery.

片段示例,因为上面不够清楚:

Snippet example because the above was not clear enough:

// Here I define some sort of solution to manipulate any object property of name "year_of_birth"

// unknown_object or a sort of prototype/constructor that intercepts any object, regardless of its scope

Object.defineProperty(unknown_object, 'year_of_birth', {
    set: function(year) {
        if (this.age && 2017 - this.age > year) {
            this.year_of_birth = 2017 - this.age;
        }
    }
});

// or

Object.defineProperty(unknown_object, 'year_of_birth', {
    get: function(year) {
        if (this.age && 2017 - this.age > year) {
            return 2017 - this.age;
        }
    }
});


// I want to modify "year_of_birth" or any other object property that is inside the below scope
// I do not have access to that scope, I cannot modify the function to allow access to its scope, I cannot do anything to it besides trying to modify the objects from the outside through some method that I am trying to find out in this question

(function() {
    var john = {
        age: 28,
        year_of_birth: 1985
    };
    var anne = {
        age: 31,
        year_of_birth: 1985
    };
}());

// Whenever a "year_of_birth" object property is read/set it should change to whichever value it has been established in the setters/getters at the top

推荐答案

在多次被告知我的要求是不可能的之后,我发现了一种通过针对 Object.prototype 来实现这一目标的方法.

After being told multiple times that what I was asking was not possible, I discovered a way to achieve this by targeting the Object.prototype.

解决方案如下,my_property"是我希望定位的任何对象属性名称.

The solution is as follows, with "my_property" being any object property name I wish to target.

Object.defineProperty(Object.prototype, "my_property", {
    set: function (value) {
        this._value = value;
    },
    get: function () {
        return "changed";
    }
});

var some_object = {};

some_object.my_property = "unchanged";


document.body.innerHTML += some_object.my_property;

My property value is: 

这篇关于如何拦截和修改任何对象的特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆