Skip to content

Instantly share code, notes, and snippets.

View luongs3's full-sized avatar

Nguyễn Phúc Lương luongs3

  • Framgia Corp
  • Hà Nội Việt Nam
View GitHub Profile
Verify Github on Galxe. gid:E4aXLUWoLrbppJ5DUeVajG
function foo() {
return this;
}
// Non-Strict Mode
foo() === window; // true
foo.apply(undefined) === window; // true
// Strict Mode
foo() === undefined; // true
function foo() {
return this;
}
// Non-Strict Mode
foo.call(1) === 1; // false.
// Bởi foo.call(1) sẽ trả ra giá trị là một object, tương đương với `new Number(1)`
// Strict Mode
foo.call(1) === 1; // true
// Non-Strict Mode
eval("var foo = 1");
foo // 1
// Strict Mode
"use strict";
eval("var foo = 1");
foo // Uncaught ReferenceError: foo is not defined
"use strict";
var foo = 1;
var bar = {foo: 2}
with (bar) {
console.log(foo); // Bạn sẽ gặp khó khăn trong việc xác định foo ở đây là biến, hay là thuộc tính của bar.
}
"use strict";
function foo(bar, baz, bar) { // Uncaught SyntaxError: Duplicate parameter name not allowed in this context
}
foo(1, 2, 3);
"use strict";
var foo = 1;
function bar() {};
delete foo; // Uncaught SyntaxError: Delete of an unqualified identifier in Strict Mode.
delete bar;
var obj = {};
Object.defineProperty(obj, "baz", {
value: 1,
configurable: false
// Without Strict Mode
NaN = "lol"; // Nothing happen
var obj = {};
Object.defineProperty(obj, 'prop', {value: 1, writable:false});
obj.prop; // => 2
obj.prop = 10;
obj.prop; // => 2
// With Strict Mode
"use strict";
// Global variable
function foo() {
bar = 1;
}
foo();
console.log(bar) // 1
// Local variable
function foo() {
function foo(){
"use strict";
// Uncaught ReferenceError: bar is not defined
bar = 0;
return bar;
}
// This will run normally
bar = 1;