【问题标题】:Locating individual class instances from values in JS从 JS 中的值定位单个类实例
【发布时间】:2020-08-22 03:07:08
【问题描述】:

我正在尝试从一个类中打印出一组坐标。此类的每个实例都有 3 个值:经度、纬度和 IP 地址。我正在从记录各种网络攻击的源和目标 IP 地址的日志文件中读取数据。当我从日志文件中读取IP地址时,JS有没有办法识别IP地址属于某个类的实例,然后根据它识别的类打印未来的类值?做这个的最好方式是什么? jQuery能解决这个问题吗?

class comp {
    constructor(ipa, long, lat){
        this.address = ipa;
        this.long = long;
        this.lat = lat;
        ips.push(address);
    }
    getlong(){
        return this.long;
    };
    getlat(){
        return this.lat;
    }
    getip(){
        return this.address;
    }
    setlatlong(inputLong, inputLat){
        this.long = inputLong;
        this.lat = inputLat;
        return;
    }
    setip(inputIP){
        this.address = inputIP;
    }
};

comp1 = new comp('10.0.0.1', '-77.050636', '38.889248');
comp2 = new comp('10.0.0.2', '-78.050636', '39.889248');
//obj = ip addresses
function process(obj){
    ///
    if (ids.includes(obj.id)) {
            ids.push(obj.id);
            hits.push( { origin : { latitude: comp1.getlat(), longitude: comp1.getlong() },
            destination : { latitude: comp2.getlat(), longitude: comp2.getlong() } } );
        }
};

【问题讨论】:

  • 你最终解决了这个问题吗?

标签: javascript jquery class oop


【解决方案1】:

我不确定我是否明白你的意思,但你可以使用static properties。不过,这是一个纯 JS 解决方案。

您可能也知道这一点,但根据 OOP 规范,我建议您使用 Pascal case 来命名类,因此我冒昧地对其进行了相应的重命名。

纯JS解决方案

class Comp {

    // declare static array
    static instances = [];

    constructor(ipa, long, lat) {
        this.address = ipa;
        this.long = long;
        this.lat = lat;

        // to access static properties, use Class.property syntax
        // populates the class's instances array with every new instance
        Comp.instances.push(this);

        ips.push(this.address);
    }

    // static method to retrieve instance based on IP
    static getInstance(ip) {
        return Comp.instances.find(v => v.address == ip, null);
    }

    getlong(){
        return this.long;
    };
    getlat(){
        return this.lat;
    }
    getip(){
        return this.address;
    }
    setlatlong(inputLong, inputLat){
        this.long = inputLong;
        this.lat = inputLat;
        return;
    }
    setip(inputIP){
        this.address = inputIP;
    }
};

comp1 = new Comp('10.0.0.1', '-77.050636', '38.889248');
comp2 = new Comp('10.0.0.2', '-78.050636', '39.889248');

console.log(Comp.getInstance('10.0.0.1')); // prints comp1

现在这是我无法真正理解的部分。检查数组是否有 IP,如果为真则再次推送它是没有意义的。

所以我想你只希望每个 IP 实例化一次,所以...

// source and target objects
function process(src, tgt){
    // gets instance of src, if it exists; else creates new one
    let comp1 = Comp.getInstance(src.ipa) || new Comp(src.ipa, src.long, src.lat);
    // gets instance of tgt, if it exists; else creates new one
    let comp2 = Comp.getInstance(tgt.ipa) || new Comp(tgt.ipa, tgt.long, src.lat);
    hits.push({
            origin: { latitude: comp1.getlat(), longitude: comp1.getlong() },
            destination : { latitude: comp2.getlat(), longitude: comp2.getlong() }
        });
};

您可以在控制台上执行console.log(Comp.instances) 来检查存储的地址和坐标。另一个可能的想法是,如果有人试图实例化一个欺骗 IP,则抛出异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多