Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Created June 7, 2023 17:34
Show Gist options
  • Save ernestlv/b29591e4e18c10f611b55292188b7f8e to your computer and use it in GitHub Desktop.
Save ernestlv/b29591e4e18c10f611b55292188b7f8e to your computer and use it in GitHub Desktop.
Provides different values of "this" depending if running in strict mode or not.
/* sloppy mode */
/* It always return an object. Boxes "this" in a primitive object if a primitive value is provided or the global object otherwise */
function fun() {
return this;
}
fun.call(2) // Number(2)
fun.bind(true)() //Boolean(true)
fun.call(null) //window object
func.apply(undefined) //window object
fun() //window object
/* stric mode */
/* it always returns the provided value for this or undefined. it never Boxes "this" in a primitive object nor assing the global object by default */
function fun() {
"use strict";
return this;
}
fun.call(2) // 2
fun.bind(true)() // true
fun.call(null) // null
fun.apply(undefined) //undefined
fun() //undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment