How to copy the prototype window?

1

How can I inherit the properties and all methods from window to an object created by me?

Since for example for this:

window.addEventListener();

I would like to make it small to use it habitually to:

var myObject = Object.create(window.prototype);
myObject.v = myObject.addEventListener;

Then then perform:

myObject.v("click",function(){});

That would be equivalent to doing:

window.addEventListener("click",function(){});

Only one syntax, shorter.

Is this possible? and how?

PS: I do not want to do one by one, this is what I mean:

function s(e,func,i = false) {
window.addEventListener(e.toString(),func, i);
}

But, completely inherit all your methods.

    
asked by Eduardo Sebastian 29.10.2017 в 03:44
source

2 answers

2

As I understand it, the only thing you want to do is create an alias of the object window and an alias to the function addEventListener

First option (recommended)

Object.defineProperty(Window.prototype, 'w', {
    configurable: true, 
    get: function(){return this}
})
Object.defineProperty(Window.prototype, 'v', {
    configurable: true, 
    value: Window.prototype.addEventListener
})

w.v('DOMContentLoaded', function(){alert('Hola mundo')}, false)

Second option (Not recommended)

var w = window
var v = Window.prototype.addEventListener.bind(w)

w.v('DOMContentLoaded', function(){alert('Hola mundo')}, false)

The first code creates:

  • An alias of the object window , now accessible through the symbol w
  • An alias for the addEventListener function, now accessible via the v symbol in the Window prototype

The second code creates:

  • An alias of the object window , now accessible through the symbol w
  • A composite function, which has been linked to the w object as the reference in this

Whichever option is chosen if you have the code

window.addEventListener(...)

It can be replaced without distinction by

w.v(...)

I do not know how useful you are to use, except for some rare kind of code obfuscation.

    
answered by 29.10.2017 / 05:28
source
0

To obtain the list of methods that has the object window just do:

for (var i in window) {
    if (typeof window[i] === 'function') {
        console.log(i);
    }
}
    
answered by 29.10.2017 в 05:03