Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function MyConstructor(data, transport) {
- this.data = data;
- transport.on('data', this.alert);
- }
- MyConstructor.prototype.alert = function() {
- alert(this.name);
- };
- function foo() {
- console.log(this);
- }
- // normal function call
- foo(); // `this` will refer to `window`
- // as object method
- var obj = {bar: foo};
- obj.bar(); // `this` will refer to `obj`
- // as constructor function
- new foo(); // `this` will refer to an object that inherits from `foo.prototype`
- function MyConstructor(data, transport) {
- this.data = data;
- var self = this;
- transport.on('data', function() {
- alert(self.data);
- });
- }
- function MyConstructor(data, transport) {
- this.data = data;
- var boundFunction = (function() { // parenthesis are not necessary
- alert(this.data); // but might improve readability
- }).bind(this); // <- here we are calling `.bind()`
- transport.on('data', boundFunction);
- }
- function MyConstructor(data, transport) {
- this.data = data;
- transport.on('data', () => alert(this.data));
- }
- array.map(callback[, thisArg])
- var arr = [1, 2, 3];
- var obj = {multiplier: 42};
- var new_arr = arr.map(function(v) {
- return v * this.multiplier;
- }, obj); // <- here we are passing `obj` as second argument
- function Foo() {
- this.data = 42,
- document.body.onclick = this.method;
- }
- Foo.prototype.method = function() {
- console.log(this.data);
- };
- function method() {
- console.log(this.data);
- }
- function Foo() {
- this.data = 42,
- document.body.onclick = this.method;
- }
- Foo.prototype.method = method;
- document.body.onclick = this.method.bind(this);
- var self = this;
- document.body.onclick = function() {
- self.method();
- };
- document.body.onclick = () => this.method();
- transport.on('data', _.bind(function () {
- alert(this.data);
- }, this));
- object.property();
- var f = object.property;
- f();
- this.saveNextLevelData(this.setAll);
- this.saveNextLevelData(this.setAll.bind(this));
- this.saveNextLevelData($.proxy(this.setAll, this));
- there are two types of scope in javascript. They are :
- 1) Global Scope
- 2) Function Scope
- --------------------------------------------------------------------------------
- - -
- - Global Scope -
- - ( globally "this" refers to window object) -
- - -
- - function outer_function(callback){ -
- - -
- - // outer function scope -
- - // inside outer function"this" keyword refers to window object - -
- - callback() // "this" inside callback also refers window object -
- - } -
- - -
- - function callback_function(){ -
- - -
- - // function to be passed as callback -
- - -
- - // here "THIS" refers to window object also -
- - -
- - } -
- - -
- - outer_function(callback_function) -
- - // invoke with callback -
- --------------------------------------------------------------------------------
- function Person(name){
- this.name = name
- this.sayNameVersion1 = function(callback){
- callback.bind(this)()
- }
- this.sayNameVersion2 = function(callback){
- callback()
- }
- this.sayNameVersion3 = function(callback){
- callback.call(this)
- }
- this.sayNameVersion4 = function(callback){
- callback.apply(this)
- }
- }
- function niceCallback(){
- // function to be used as callback
- var parentObject = this
- console.log(parentObject)
- }
- var p1 = new Person('zami') // create an instance of Person constructor
- this.sayNameVersion1 = function(callback){
- callback.bind(this)()
- }
- this.sayNameVersion2 = function(callback){
- callback()
- }
- p1.sayNameVersion1(niceCallback) // pass simply the callback and bind happens inside the sayNameVersion1 method
- p1.sayNameVersion2(niceCallback.bind(p1)) // uses bind before passing callback
- this.sayNameVersion3 = function(callback){
- callback.call(this)
- }
- p1.sayNameVersion3(niceCallback)
- this.sayNameVersion4 = function(callback){
- callback.apply(this)
- }
- p1.sayNameVersion4(niceCallback)
- setTimeout(function(){
- this.methodName();
- }.bind(this), 2000);
- function MyConstructor(data, transport) {
- this.data = data;
- var self = this;
- transport.on('data', function () {
- alert(self.data);
- });
- }
- function MyConstructor(data, transport) {
- this.data = data;
- transport.on('data',()=> {
- alert(this.data);
- });
- }
- function MyConstructor(data, transport) {
- this.data = data;
- transport.on('data',(function() {
- alert(this.data);
- }).bind(this);
- var obj = {
- handleEvent(e) {
- // always true
- console.log(this === obj);
- }
- };
- document.body.addEventListener('click', obj);
- class someView {
- onSomeInputKeyUp = (event) => {
- console.log(this); // this refers to correct value
- // ....
- someInitMethod() {
- //...
- someInput.addEventListener('input', this.onSomeInputKeyUp)
- function foo(){
- var a =2 ;
- this.bar();
- }
- function bar (){
- console.log(this.a);
- }
- foo(); //undefined, as function call refer to global scope because it’s the invocation context of the function (window.foo()
- var myFunction = function() {
- console.log(this);
- }
- myFunction(); // Window
- var myFunction = function() {
- console.log(this.a);
- }
- var a = 5 ;
- myFunction(); //5
- function MyConstructor(data, transport) {
- this.data = data;
- transport.on('data', () => {
- alert(this.data);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement