Skip to content

Instantly share code, notes, and snippets.

@fdrobidoux
Created July 28, 2016 21:23
Show Gist options
  • Save fdrobidoux/249ca71e5ea6e18f4d0282e8bff8868c to your computer and use it in GitHub Desktop.
Save fdrobidoux/249ca71e5ea6e18f4d0282e8bff8868c to your computer and use it in GitHub Desktop.
Some tests I've done involving `this` scopes and `function` object properties.
function Foo() {
this.bar = "foo";
}
Foo.prototype.use = (function (_instance, _name) {
return Object.assign(function use() {
// console.log("false: ", _fn.hello);
// console.log("false: ", _fn.aBar);
console.log("true: ", _instance.bar);
console.log("true: ", this.use.hello);
console.log("true: ", this[_name].hello);
console.log("true: ", use.aBar);
console.log("false: ", this.bar);
console.log("is Foo: ", Object.getPrototypeOf(this));
}, {
hello: "world!",
aBar: this.bar, // !!!
// Both give an error!
// helloWithin: this["use"]["hello"] || _instance[_name]["hello"]
});
})(this, "use");
Foo.prototype.baz = function baz() {
return "help";
}
var foo = new Foo();
foo.use();
console.log(foo.bar);
console.log(Object.getOwnPropertyNames(foo));
console.log(foo.use.hello);
console.log(foo.use.aBar);
// console.log(foo.use.helloWithin);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment