Skip to content

Instantly share code, notes, and snippets.

@samson-sham
Last active April 10, 2019 08:28
Show Gist options
  • Save samson-sham/83b547f8acdc34cbab5b63fdaa97634a to your computer and use it in GitHub Desktop.
Save samson-sham/83b547f8acdc34cbab5b63fdaa97634a to your computer and use it in GitHub Desktop.
Live with it
/**
* { testground: '1.0.0',
npm: '6.8.0',
ares: '1.14.0',
cldr: '33.1',
http_parser: '2.8.0',
icu: '62.1',
modules: '64',
napi: '3',
nghttp2: '1.32.0',
node: '10.8.0',
openssl: '1.1.0h',
tz: '2018e',
unicode: '11.0',
uv: '1.22.0',
v8: '6.7.288.49-node.19',
zlib: '1.2.11' }
*/
const
objectA = {
"prop": "property A"
};
const
callProp = function() {
console.log(this.prop);
},
callPropByArrow = () => {
console.log(this.prop);
};
callProp.bind(objectA)(); // property A
callPropByArrow.bind(objectA)(); // undefined
console.log("=================Methods");
const
objectC = {
"prop": "property C. Object with declarative arrow syntax",
"method": () => {
console.log(this.prop);
}
},
objectD = {
"prop": "property D. Object with declarative function syntax",
"method": function() {
console.log(this.prop);
}
},
objectE = {
"prop": "property E. Object with ES6 method syntax",
"method"() {
console.log(this.prop);
}
};
objectC.method(); // undefined
objectD.method(); // property D
objectE.method(); // property E
console.log("=================Bindings");
objectC.method.bind(objectA)(); // undefined
objectD.method.bind(objectA)(); // property A
objectE.method.bind(objectA)(); // property A
console.log("=================Class");
class ClassF {
static get prop() {
return "property F. Object with ES6 Class declaration"
}
static staticMethod() {
console.log(this.prop);
}
method() {
console.log(this.constructor.prop);
}
}
const objectF = new ClassF();
ClassF.staticMethod(); // property F
objectF.method(); // property F
console.log("=================ES5 Prototypes");
function ClassG() {
this.method = (function() {
console.log(this.prop);
}).bind(this);
}
ClassG.prototype.prop = "propery G. Object with Function & Prototype declaration";
ClassG.staticMethod = function() {
console.log(this.prototype.prop);
};
const objectG = new ClassG();
ClassG.staticMethod(); // property G
objectG.method(); // property G
console.log("=================Promises");
(async () => {
// Promises, thennables are executed on event loop without scope
await Promise.resolve(true)
.then(objectC.method) // undefined
.then(objectD.method) // undefined
.then(objectE.method) // undefined
// .then(ClassF.staticMethod) // crash
// .then(objectF.method) // crash
// .then(ClassG.staticMethod) // crash
.then(objectG.method); // property G
console.log("=================Bluebird");
// Bluebird won't save you
const Bluebird = require('bluebird');
await Bluebird.resolve(true)
.then(objectC.method) // undefined
.then(objectD.method) // undefined
.then(objectE.method) // undefined
// .then(ClassF.staticMethod) // crash
// .then(objectF.method) // crash
// .then(ClassG.staticMethod) // crash
.then(objectG.method); // property G
console.log("=================Arrow Bindings");
await Promise.resolve(true)
.then(() => objectC.method()) // undefined
.then(() => objectD.method()) // property D
.then(() => objectE.method()); // property E
console.log("=================Object Bindings");
await Promise.resolve(true)
.then(objectC.method.bind(objectC)) // undefined
.then(objectD.method.bind(objectD)) // property D
.then(objectE.method.bind(objectE)) // property E
.then(ClassF.staticMethod.bind(ClassF)) // property F
.then(objectF.method.bind(objectF)) // property F
.then(ClassG.staticMethod.bind(ClassG)) // property G
.then(objectG.method.bind(objectG)); // property G
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment