Skip to content

Instantly share code, notes, and snippets.

@nomyfan
Created February 6, 2023 06:53
Show Gist options
  • Save nomyfan/39b8610ae507b5768198776d4f1a889d to your computer and use it in GitHub Desktop.
Save nomyfan/39b8610ae507b5768198776d4f1a889d to your computer and use it in GitHub Desktop.
JavaScript strict mode
/**
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#strict_mode_for_modules
* @param a
* @param b
*/
function esmIsStrictByDefault(a: number, b = 2) {
return a + b;
}
/**
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#strict_mode_for_functions
* @param a
*/
function applyToBodyStrictParamsToBeSimple(a = 1) {
'use strict';
return a + 2;
}
/**
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#strict_mode_for_classes
*/
function classBodyIsInStrictMode() {
class C1 {
constructor() {
delete Object.prototype;
}
test() {
delete Object.prototype;
}
}
const C2 = class {
test() {
// delete Object.prototype;
}
}
}
function assigningTOUndeclaredVariables() {
// let mistypeVariable = 1;
mistypeVariable = 12;
}
function failingToAssignToObjectProperties() {
// NaN is non-writable global variable. In sloppy-mode, it does nothing,
// in strict-mode, it throws an error.
NaN = 1;
}
function failingToDeleteObjectProperties() {
delete Object.prototype;
delete [].length;
var x;
// Delete on a variable name
delete x;
}
function duplicatePropertyNames() {
const o = { p: 1, p: 2};
}
function removalOfTheWithStatement() {
const x = 12;
const obj = {};
with(obj) {
x = 23;
}
}
function nonLeakingEval() {
var x = 12;
var evalX = eval("'use strict'; var x = 43; x");
console.log(x);
console.log(evalX);
}
function blockScopedFunction() {
{
function inner() {
console.log('local function')
}
}
inner();
}
function noThisSubstitution() {
function returnThis() {
return this;
}
console.assert(returnThis() === undefined);
console.assert(returnThis.apply(null) === null);
console.assert(returnThis.call(undefined) === undefined);
console.assert(returnThis.bind(true)());
}
function removalOfStackWalkingProperties() {
removalOfStackWalkingProperties.caller;
removalOfStackWalkingProperties.arguments;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment