Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var testObject = {
- oThis : this,
- testVariable : "somestring",
- init : function(){
- console.log(this.testVariable); // outputs testVariable as expected
- this.testObject.submit(function(){
- var anotherThis = this;
- console.log(this.testVariable) // undefined
- console.log(oThis.testVariable) // undefined
- console.log(testObject.testVariable) // outputs testVariable
- console.log(anotherThis.testVariable) // undefined
- }
- }
- init : function(){
- this.testObject.submit = this.submitForm;
- },
- submitForm : function(){
- // do validation here
- console.log(this.testVariable) // outputs testvariable
- .
- .
- .
- return valid;
- }
- init: function() {
- var _this = this;
- this.testObject.submit(function() {
- console.log(_this.testVariable); // outputs testVariable
- });
- }
- init: function() {
- // whatever
- var instance = this;
- instance.submitForm = function() {
- console.log(instance.testVariable);
- // ...
- };
- }
- var testInstance = new testObject();
- testInstance.oThis;
- init: function(){
- var self = this; // this allows you to access the parent object from different contexts
- this.testObject.submit(function(){
- console.log(self.testVariable);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement