- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: javascript-best-practices/no-unnecessary-bind
Language: JavaScript
Severity: Warning
Category: Performance
This rule advises against the use of unnecessary .bind()
calls in JavaScript. The .bind()
method is used to create a new function that, when called, has its this
keyword set to the provided value. However, unnecessary .bind()
calls can lead to confusion about what this
refers to, and they can also have a negative impact on performance.
The importance of this rule lies in the clarity and efficiency of your code. Unnecessary .bind()
calls can make your code harder to understand and maintain. Moreover, they can result in slower code execution, as each .bind()
call creates a new function.
To avoid violating this rule, only use .bind()
when necessary, for example, when you need to set the this
value for a function. Use arrow functions if you want to preserve the this
value from the enclosing context. Also, avoid using .bind()
in a loop, as it can lead to performance issues. Instead, create the bound function once and reference it within the loop.
const func = function () {
foo();
}.bind(bar);
const func = (() => {
foo();
}).bind(bar);
const func = (() => {
this.foo();
}).bind(bar);
const func = function () {
(function () {
this.foo();
}());
}.bind(bar);
const func = function () {
function foo() {
this.bar();
}
}.bind(baz);
const func = function () {
this.foo();
}.bind(bar);
const func = function (baz) {
return baz + 1;
}.bind(foo, bar);
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products