Call, Bind and Apply Methods
Bind( )
The bind method creates a new function and sets the this
keyword to the specified object.
Syntax:
function.bind(thisArg, optionalArguments)
Call ( )
The call method sets the this
inside the function and immediately executes that function.
The difference between call()
and bind()
is that the call()
sets the this
keyword and executes the function immediately and it does not create a new copy of the function, while the bind()
creates a copy of that function and sets the this
keyword.
Syntax:
function.call(thisArg, arg1, agr2, ...)
Apply ( )
The apply()
method is similar to call()
. The difference is that the apply()
method accepts an array of arguments instead of comma separated values.
Syntax:
function.apply(thisArg, [argumentsArr])
JavaScript Use Strict
"use strict";
Defines that
JavaScript code should be executed in
"strict mode"."use strict"
directive was new in ECMAScript version 5.
It is not a statement, but a literal expression, ignored by earlier versions of JavaScript.
The purpose of "use strict"
is to indicate that the code should be executed in "strict mode".
All modern browsers support "use strict" except Internet Explorer 9 and lower:
The strict mode in JavaScript does not allow following things:
- Use of undefined variables
- Use of reserved keywords as variable or function name
- Duplicate properties of an object
- Duplicate parameters of function
- Assign values to read-only properties
- Modifying arguments object
- Octal numeric literals
- with statement
- eval function to create a variable
Why Strict Mode?
Strict mode makes it easier to write "secure" JavaScript.
Strict mode changes previously accepted "bad syntax" into real errors.
As an example, in normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.
In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties.
In strict mode, any assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object, will throw an error.
Keywords reserved for future JavaScript versions can NOT be used as variable names in strict mode.
These are:
- implements
- interface
- let
- package
- private
- protected
- public
- static
- yield
== and ===
What is == in JavaScript?
Double equals (==) is a comparison operator, which transforms the operands having the same type before comparison.
So, when you compare string with a number, JavaScript converts any string to a number. An empty string is always converts to zero. A string with no numeric value is converts to NaN (Not a Number), which returns false.
What is === in JavaScript?
=== (Triple equals) is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 2 with “2” using ===, then it will return a false value.