Skip to content

Consider not putting in global space functions that are named the same as window. methods. #2689

@danon

Description

@danon

Some applications take advantage of the fact that when you want to call a method on window., you don't need to prefix it, and call it like that:

window.addEventListener('load', callback); // instead of doing that:
addEventListener('load', callback);        // they do that

If nothing overrides that, it works fine. When I write a library using solid.js and I bundle it and put it on a website that uses those tricks, I get error: node.addEventListener() is not a function.

I traced it down to solid, because solid adds a global function addEventListener():

function addEventListener(node, name, handler, delegate) {
	if (delegate) if (Array.isArray(handler)) {
		node[`$$${name}`] = handler[0];
		node[`$$${name}Data`] = handler[1];
	} else node[`$$${name}`] = handler;
	else if (Array.isArray(handler)) {
		const handlerFn = handler[0];
		node.addEventListener(name, handler[0] = (e) => handlerFn.call(node, handler[1], e));
	} else node.addEventListener(name, handler, typeof handler !== "function" && handler);
}

Of course you can add global functions, but do you think you could name them something that's not also on window., so people don't mistakenly call it?

I tried it out, when I rename your addEventListener() to something else in node_modules/ my error goes away.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions