Skip to main content
Adapted for CommonMark (as a result, the diff looks more extensive than it really is - use view "Side-by-side Markdown" to compare), etc.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

##Here are several ways to access the parent context inside a child context -

Here are several ways to access the parent context inside a child context -

  1. You can use the bind() function.
  2. Store a reference to context/this inside another variable (see the below example).
  3. Use ES6 Arrow functions.
  4. Alter the code, function design, and architecture - for this you should have command over design patterns in JavaScript.

#1. Use the bind() function

1. Use the bind() function

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', ( function () {
        alert(this.data);
    }).bind(this) );
}
// Mock transport object
var transport = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};
// called as
var obj = new MyConstructor('foo', transport);

If you are using Underscore.js - http://underscorejs.org/#bind

transport.on('data', _.bind(function () {
    alert(this.data);
}, this));

#2 Store a reference to context/this inside another variable

2. Store a reference to context/this inside another variable

function MyConstructor(data, transport) {
  var self = this;
  this.data = data;
  transport.on('data', function() {
    alert(self.data);
  });
}

#3 Arrow function

3. Arrow function

function MyConstructor(data, transport) {
  this.data = data;
  transport.on('data', () => {
    alert(this.data);
  });
}

##Here are several ways to access the parent context inside a child context -

  1. You can use the bind() function.
  2. Store a reference to context/this inside another variable (see the below example).
  3. Use ES6 Arrow functions.
  4. Alter the code, function design, and architecture - for this you should have command over design patterns in JavaScript.

#1. Use the bind() function

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', ( function () {
        alert(this.data);
    }).bind(this) );
}
// Mock transport object
var transport = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};
// called as
var obj = new MyConstructor('foo', transport);

If you are using Underscore.js - http://underscorejs.org/#bind

transport.on('data', _.bind(function () {
    alert(this.data);
}, this));

#2 Store a reference to context/this inside another variable

function MyConstructor(data, transport) {
  var self = this;
  this.data = data;
  transport.on('data', function() {
    alert(self.data);
  });
}

#3 Arrow function

function MyConstructor(data, transport) {
  this.data = data;
  transport.on('data', () => {
    alert(this.data);
  });
}

Here are several ways to access the parent context inside a child context -

  1. You can use the bind() function.
  2. Store a reference to context/this inside another variable (see the below example).
  3. Use ES6 Arrow functions.
  4. Alter the code, function design, and architecture - for this you should have command over design patterns in JavaScript.

1. Use the bind() function

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', ( function () {
        alert(this.data);
    }).bind(this) );
}
// Mock transport object
var transport = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};
// called as
var obj = new MyConstructor('foo', transport);

If you are using Underscore.js - http://underscorejs.org/#bind

transport.on('data', _.bind(function () {
    alert(this.data);
}, this));

2. Store a reference to context/this inside another variable

function MyConstructor(data, transport) {
  var self = this;
  this.data = data;
  transport.on('data', function() {
    alert(self.data);
  });
}

3. Arrow function

function MyConstructor(data, transport) {
  this.data = data;
  transport.on('data', () => {
    alert(this.data);
  });
}
Active reading [<https://www.youtube.com/watch?v=1Dax90QyXgI&t=17m54s> <https://en.wikipedia.org/wiki/JavaScript> <https://en.wikipedia.org/wiki/Underscore.js>].
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

##Here are several ways to access the parent context inside a child context -

  1. You can use the bind() function.
  2. Store a reference to context/this inside another variable  (see the below example).
  3. Use ES6 Arrow functions.
  4. Alter the code/function, function design/architecture, and architecture - for this you should have command over  design patterns in javascriptJavaScript.

#1. Use the bind() function

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', ( function () {
        alert(this.data);
    }).bind(this) );
}
// Mock transport object
var transport = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};
// called as
var obj = new MyConstructor('foo', transport);

If you are using underscore.jsUnderscore.js - http://underscorejs.org/#bind

transport.on('data', _.bind(function () {
    alert(this.data);
}, this));

#2 Store a reference to context/this inside another variable

function MyConstructor(data, transport) {
  var self = this;
  this.data = data;
  transport.on('data', function() {
    alert(self.data);
  });
}

#3 Arrow function

function MyConstructor(data, transport) {
  this.data = data;
  transport.on('data', () => {
    alert(this.data);
  });
}

##Here are several ways to access parent context inside child context -

  1. You can use bind() function.
  2. Store reference to context/this inside another variable(see below example).
  3. Use ES6 Arrow functions.
  4. Alter code/function design/architecture - for this you should have command over  design patterns in javascript.

#1. Use bind() function

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', ( function () {
        alert(this.data);
    }).bind(this) );
}
// Mock transport object
var transport = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};
// called as
var obj = new MyConstructor('foo', transport);

If you are using underscore.js - http://underscorejs.org/#bind

transport.on('data', _.bind(function () {
    alert(this.data);
}, this));

#2 Store reference to context/this inside another variable

function MyConstructor(data, transport) {
  var self = this;
  this.data = data;
  transport.on('data', function() {
    alert(self.data);
  });
}

#3 Arrow function

function MyConstructor(data, transport) {
  this.data = data;
  transport.on('data', () => {
    alert(this.data);
  });
}

##Here are several ways to access the parent context inside a child context -

  1. You can use the bind() function.
  2. Store a reference to context/this inside another variable  (see the below example).
  3. Use ES6 Arrow functions.
  4. Alter the code, function design, and architecture - for this you should have command over design patterns in JavaScript.

#1. Use the bind() function

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', ( function () {
        alert(this.data);
    }).bind(this) );
}
// Mock transport object
var transport = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};
// called as
var obj = new MyConstructor('foo', transport);

If you are using Underscore.js - http://underscorejs.org/#bind

transport.on('data', _.bind(function () {
    alert(this.data);
}, this));

#2 Store a reference to context/this inside another variable

function MyConstructor(data, transport) {
  var self = this;
  this.data = data;
  transport.on('data', function() {
    alert(self.data);
  });
}

#3 Arrow function

function MyConstructor(data, transport) {
  this.data = data;
  transport.on('data', () => {
    alert(this.data);
  });
}
links changes.
Source Link
Mohan Dere
  • 4.6k
  • 1
  • 27
  • 21

##Here are several ways to access parent context inside child context -

  1. You can use bindbind() function - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind.
  2. Store reference to context/this inside another variable(see below example).
  3. Use ES6 Arrow functions - https://derickbailey.com/2015/09/28/do-es6-arrow-functions-really-solve-this-in-javascript/Arrow functions.
  4. Alter code/function architecturedesign/architecture - for this you should have commands on design patterns in javascript - command over https://addyosmani.com/resources/essentialjsdesignpatterns/book/design patterns in javascript.

#1. Use bind() function

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', ( function () {
        alert(this.data);
    }).bind(this) );
}
// Mock transport object
var transport = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};
// called as
var obj = new MyConstructor('foo', transport);

If you are using underscore.js - http://underscorejs.org/#bind

transport.on('data', _.bind(function () {
    alert(this.data);
}, this));

#2 Store reference to context/this inside another variable

function MyConstructor(data, transport) {
  var self = this;
  this.data = data;
  transport.on('data', function() {
    alert(self.data);
  });
}

#3 Arrow function

function MyConstructor(data, transport) {
  this.data = data;
  transport.on('data', () => {
    alert(this.data);
  });
}

##Here are several ways to access parent context inside child context -

  1. You can use bind() function - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind
  2. Store reference to context/this inside another variable
  3. Use ES6 Arrow functions - https://derickbailey.com/2015/09/28/do-es6-arrow-functions-really-solve-this-in-javascript/
  4. Alter code/function architecture - for this you should have commands on design patterns in javascript - https://addyosmani.com/resources/essentialjsdesignpatterns/book/

#1. Use bind() function

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', ( function () {
        alert(this.data);
    }).bind(this) );
}
// Mock transport object
var transport = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};
// called as
var obj = new MyConstructor('foo', transport);

If you are using underscore.js - http://underscorejs.org/#bind

transport.on('data', _.bind(function () {
    alert(this.data);
}, this));

#2 Store reference to context/this inside another variable

function MyConstructor(data, transport) {
  var self = this;
  this.data = data;
  transport.on('data', function() {
    alert(self.data);
  });
}

#3 Arrow function

function MyConstructor(data, transport) {
  this.data = data;
  transport.on('data', () => {
    alert(this.data);
  });
}

##Here are several ways to access parent context inside child context -

  1. You can use bind() function.
  2. Store reference to context/this inside another variable(see below example).
  3. Use ES6 Arrow functions.
  4. Alter code/function design/architecture - for this you should have command over design patterns in javascript.

#1. Use bind() function

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', ( function () {
        alert(this.data);
    }).bind(this) );
}
// Mock transport object
var transport = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};
// called as
var obj = new MyConstructor('foo', transport);

If you are using underscore.js - http://underscorejs.org/#bind

transport.on('data', _.bind(function () {
    alert(this.data);
}, this));

#2 Store reference to context/this inside another variable

function MyConstructor(data, transport) {
  var self = this;
  this.data = data;
  transport.on('data', function() {
    alert(self.data);
  });
}

#3 Arrow function

function MyConstructor(data, transport) {
  this.data = data;
  transport.on('data', () => {
    alert(this.data);
  });
}
added 315 characters in body
Source Link
Mohan Dere
  • 4.6k
  • 1
  • 27
  • 21
Loading
added 125 characters in body
Source Link
Mohan Dere
  • 4.6k
  • 1
  • 27
  • 21
Loading
Source Link
Mohan Dere
  • 4.6k
  • 1
  • 27
  • 21
Loading