user802599
user802599

Reputation: 828

Can I disable default JavaScript operators or functions?

I have been adding Feature Policy Headers to my pages and was wondering if there is a way to do something similar for functions or operators in Javascript?

So, for example, I try to always use triple equals and never use double equals. So can I disable the double equals operator?

I am already using JSDoc with TypeScript checking and linting to check my code when I am writing it. I am interested in finding out if it is possible to disable operators and default functions when the code runs in the browser.

If it is possible to override the operators to remove them and remove some of the default functions, I was going to try and see if removing these and just using a subset of functions would speed up the run time for some code.

Upvotes: 0

Views: 181

Answers (2)

CertainPerformance
CertainPerformance

Reputation: 370839

It's impossible to disable Javascript operators, like ==, but most built-in functions can be changed. For example, for document.getElementsByClassName, simply assign your own custom function to document.getElementsByClassName:

document.getElementsByClassName = () => {
  throw new Error('Use querySelectorAll instead');
};

const divs = document.getElementsByClassName('div');

You can also prevent getElementsByClassName from being called on elements by assigning over Element.prototype:

Element.prototype.getElementsByClassName = () => {
  throw new Error('Use querySelectorAll instead');
};
const childs = div.getElementsByClassName('foo');
<div id="div"></div>

Upvotes: 0

jsdeveloper
jsdeveloper

Reputation: 4045

This is a task for a linter, imo:

https://eslint.org/docs/rules/eqeqeq

Upvotes: 2

Related Questions