Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function() {
- this.a = "a"; // This obc creates a variable called "a" in this function, like a field.
- }
- class A {
- test() {
- this.a = "a2"; // Same here. The "a" variable in the other function is not the same as this one. But...
- }
- }
- class B {
- test() {
- this.a = function() {
- this.a = "a3"; // HOLD UP! the "this" identifier IS NOT referencing the class. It is referencing the function, like how i showed it in the first example!!!!!
- }; // "a" is now a function. you can call it if you want.
- this.a();
- }
- test2() { // If you want to fix that problem you need to do this:
- this.a = function() {
- this.a = "a3";
- }.bind(this); // This bind function can be used for a ton of things, but you are passing in an instance of "this" that you want to use. You can also do stuff like:
- this.b = function(myName) {
- console.log(myName); // "doggo"
- }.bind(this, "doggo");
- // If you aren't really referencing "this", you can just pass in null as the first parameter
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement