简体   繁体   English

是否可以遍历JavaScript伪类的所有实例?

[英]Is it possible to iterate over all instances of a JavaScript pseudoclass?

I'm looking for a way (preferably without constructing a container they are added to) to loop through all instances of a JavaScript pseudoclass without looping nesting the instances off and looping through all children recursively of the window object. 我正在寻找一种方法(最好是不构造将它们添加到的容器)循环遍历JavaScript伪类的所有实例,而不循环嵌套实例并递归遍历window对象的所有子级。 Is this possible or do I have no recourse but to create an array holding all instances of any pseudoclass I want to access all the instances of? 这是否可行,或者除了创建要保存要访问其所有实例的任何伪类的所有实例的数组之外,我是否无权求助?

It's not possible, since the an object does not know what other objects inherit from it. 这是不可能的,因为一个对象不知道还有其他对象从该对象继承。 It's a unidirectional relation (from object/instance to prototype). 这是一个单向关系(从对象/实例到原型)。

And not all objects are reachable via recursing over window . 并非所有对象都可以通过在window上递归来访问。 You don't have access to local variables in functions. 您无权访问函数中的局部变量。

You have to keep track of created instances manually. 您必须手动跟踪创建的实例。

Managed to solve this with the following code (and got rid of the need to create a dummy instance of objects for each inheritance in an inheritance chain): 设法使用以下代码解决了这个问题(并且无需为继承链中的每个继承创建对象的虚拟实例):

Object.defineProperty(Object.prototype, 'constructing', {
    enumerable: false,
    writable: true,
    value: false
});
Object.defineProperty(Object.prototype, 'construct', {
    enumerable: false,
    get: function () {
        if ((this === Object) || (this.constructor === Object)) { return; }
        if (this.constructing === false) {
            this.constructing = this.__proto__.constructor;
        }
        if (this.constructing.hasOwnProperty('instances')) { this.constructing.instances.push(this); }
        var c = this.constructing;
        if ('base' in c) {
            this.constructing = c.base;
            this.constructing.call(this);
        }
        if (c !== this.__proto__.constructor) {
            for (var i in c.prototype) {
                if (!this.__proto__.hasOwnProperty(i)) { this.__proto__[i] = c.prototype[i]; }
            }
        }
    }
});

function A() {
    this.construct;
    this.foo = 'foo';
}
function A_bar() { console.log(this.foo + 'foo'); }
A.prototype.constructor = A;
A.prototype.bar = A_bar;
A.instances = new Array();

function B() {
    this.construct;
    this.foo = 'bar';
    var base = this.__proto__.constructor.base.prototype;
}
function B_bar() { console.log('bar'); }
B.base = A;
B.prototype.constructor = B;
B.prototype.bar = B_bar;
B.instances = new Array();

document.write(A.instances.length);
document.write(B.instances.length);
var a = new A();
document.write(a.foo);
document.write(A.instances.length);
document.write(B.instances.length);
var b = new B();
document.write(b.foo);
document.write(A.instances.length);
document.write(B.instances.length);
var c = new B();
document.write(c.foo);
document.write(A.instances.length);
document.write(B.instances.length);
a.bar();
b.bar();
c.bar();

Output: 输出:

00foo10bar21bar32

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM