#Dumb question: prototype-less, and how?

11 messages · Page 1 of 1 (latest)

stray void
#

for some reason, when entering this into a console.log on a web browser, it STILL inserts a prototype :'3

function KeepOnly(){
    delete this.__proto__;
    this.properties = arguments[0];
    this.arrKeep = arguments[1];
    this.result = {};

    for (this.i in this.properties)
        for(this.j in this.arrKeep)
            if (this.i == this.j)(
                console.log(this.i + " : " + this.j),
                this.result[this.i] = this.properties[this.i]
            );

    delete this.properties;
    delete this.arrKeep;
    delete this.i;
    delete this.j;
    return this.result;
}

function Splitter(){
    //this.__proto__ = null;
    delete this.__proto__;
    this.stringy = arguments[0];
    this.splitter = arguments[1];
    this.matcher = arguments[2];
    this.hold=[[]];
    this.result = null;

    for (this.i in this.stringy)
        if (this.stringy[this.i] != this.splitter)
            this.hold[this.hold.length - 1] += this.stringy[this.i];
        else this.hold[this.hold.length] = [];

    this.result = this.hold;
    if (!!this.matcher)
        for (this.i in this.hold)
            if (this.hold[this.i] != this.matcher)
                this.result = false;
            else {
                this.result = true;
                break;
            }

    /*delete this.stringy;
    delete this.splitter;
    delete this.matcher;
    delete this.hold;
    delete this.i;

    return this.result;*/

    return new KeepOnly(this, ['result']);
}```

Any help would be appreciated!  I tried moving ``this.__proto__`` around and even tried ``delete this.__proto__`` to no avail
#

if i removed the comments, im able to show raw data without prototypes

urban topaz
urban topaz
#

instead of removing --proto-- you could reduce on ownProperties, and all delete is not necessary IMHO, that would be a job for the gc. The usage of thisin that purely functional approach also is unusual. I'd just use local variables.

#

sth like

function keepOnly(obj = {}, keysToKeep = []) {
    const result = {};
    for (const [key, value] of Object.entries(obj)) {
        if (keysToKeep.includes(key)) result[key] = value;
    }
    return result;
}


for the first part (I just used named args, as I find it makes the code more readable, but this way uses Array.includes() , using Set might be more efficient - could be done even in an ugly oneliner, but...)

shut gorge
#

To first directly address the main questions, the proto is deprecated. The proper way to remove the prototype for KeepOnly() would be:

Object.setPrototypeOf(this, null);

However, it doesn't matter at all if KeepOnly() has a prototype, because the thing you are returning is a totally separate object named "result". I'm not sure if you don't understand the correct usage of constructor functions or if you are deliberately writing bad code intended to be confusing, which I have seen you do before. A normal constructor function doesn't use the return keyword at all. What you actually have are two different objects created. One is the usual one that a constructor function would create and the other is the one you returned. Your code only attempts to delete the prototype of the object that the constructor creates, but the return statement is returning a different object, and there is no code that even attempts to delete the prototype of that object. Because you have a return statement, the normal object that would have been constructed is never returned.

urban topaz
stray void
#

And you’re right to call me out for cursed JavaScript, for that was the intent for this specific flavor :,3

#

I just might’ve misunderstood how the prototype was proceeding and if return was to blame

shut gorge