Last active
November 17, 2019 22:03
-
-
Save NSLog0/2f1a37055757a485658f336e3bf5c6f9 to your computer and use it in GitHub Desktop.
this binding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo() { | |
console.log( this.a ); | |
} | |
var a = 2; | |
foo(); // 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo() { | |
"use strict"; | |
console.log( this.a ); | |
} | |
var a = 2; | |
foo(); // TypeError: `this` is `undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo() { | |
console.log( this.a ); | |
} | |
var obj = { | |
a: 2, | |
foo: foo | |
}; | |
var bar = obj.foo; // function reference/alias! | |
var a = "oops, global"; // `a` also property on global object | |
bar(); // "oops, global" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo() { | |
console.log( this.a ); | |
} | |
var obj = { | |
a: 2, | |
foo: foo | |
}; | |
var bar = obj.foo; // function reference/alias! | |
var a = "oops, global"; // `a` also property on global object | |
bar.call(obj) //2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person = { | |
firstName: 'John', | |
lastName: 'Doe', | |
sayHi: function(){ | |
console.log('Hello '+ this.firstName+' '+this.lastName); | |
} | |
} | |
person.sayHi(); //Hello John Doe |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hi(ctx){ | |
ctx.sayHi =function(){ | |
console.log('Hello '+ this.firstName+' '+this.lastName); | |
} | |
} | |
var person1 = { | |
firstName: 'John', | |
lastName: 'Doe' | |
} | |
var person2= { | |
firstName: 'John', | |
lastName: 'Roe' | |
} | |
hi(person1); | |
hi(person2); | |
person1.sayHi(); //Hello John Doe | |
person2.sayHi(); //Hello John Roe |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//[1] | |
function person (){ | |
return { | |
name: 'John Doe', | |
print : function(){ | |
console.log(this.name); | |
} | |
} | |
} | |
var obj = person('John Doe'); | |
obj.print(); //John Doe | |
//[2] | |
function person (name , skill){ | |
return { | |
name: name , | |
print : function(){ | |
console.log(this.name); | |
}, | |
skill: { | |
name: skill, | |
print : function(){ | |
console.log(this.name); | |
} | |
} | |
} | |
} | |
var obj = person('John Doe', 'Java, AngularJS, NodeJs'); | |
obj.skill.print(); //Java, AngularJS, NodeJs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//[1] | |
function print1() { | |
console.log( this.name); | |
} | |
//[2] | |
function print2(arg) { | |
console.log( this.name + ' '+ arg); | |
} | |
//[3] | |
function print3(arg1, arg2) { | |
console.log( this.name + ' ' + arg1 + ' ' + arg2 ); | |
} | |
var john = { | |
name: 'John Doe', | |
skill: 'Java, C' | |
} | |
print1.call(john); //John Doe | |
print2.call(john , 'age 24'); //John Doe age 24 | |
print3.call(john , 'age 24', 'live in USA'); //John Doe age 24 live in USA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//[3] | |
function print3(arg1, arg2) { | |
console.log( this.name + ' ' + arg1 + ' ' + arg2 ); | |
} | |
var john = { | |
name: 'John Doe', | |
skill: 'Java, C' | |
} | |
var data = [ 'age 24', 'live in USA']; | |
print3.apply(john ,data); //John Doe age 24 live in USA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//[3] | |
function print3(arg1, arg2) { | |
console.log( this.name + ' ' + arg1 + ' ' + arg2 ); | |
} | |
var john = { | |
name: 'John Doe', | |
skill: 'Java, C' | |
} | |
var data = [ 'age 24', 'live in USA']; | |
var fn1 = print3.bind(john ,data); | |
var fn2 = print3.bind(john ,'age 24', 'live in USA'); | |
fn1(); //John Doe age 24 live in USA | |
fn2(); //John Doe age 24 live in USA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Rectangle { | |
private int x, y; | |
private int width, height; | |
public Rectangle() { | |
this(0, 0, 1, 1); | |
} | |
public Rectangle(int width, int height) { | |
this(0, 0, width, height); | |
} | |
public Rectangle(int x, int y, int width, int height) { | |
this.x = x; | |
this.y = y; | |
this.width = width; | |
this.height = height; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Rectangle { | |
private int x, y; | |
private int width, height; | |
public Rectangle(int x, int y, int width, int height) { | |
this.x = x; | |
this.y = y; | |
this.width = width; | |
this.height = height; | |
} | |
public void print_me(){ | |
System.out.print(this.x +" "+this.y+" "+this.width+" "+this.height); | |
} | |
} | |
public class App { | |
public static void main(String [] args){ | |
Rectangle rec = new Rectangle (0,0,45,45); | |
rec.print_me(); //0 0 45 45 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Rectangle(x, y , width , height) { | |
this.x = x; | |
this.y = y; | |
this.width = width; | |
this.height = height; | |
} | |
Rectangle.prototype = { | |
print_me: function(){ | |
console.log(this.x +" "+this.y+" "+this.width+" "+this.height); | |
} | |
} | |
var rec = new Rectangle(0, 0, 45, 45); | |
rec.print_me(); //0 0 45 45 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rectangle.prototype = { | |
print_me: function(){ | |
console.log(this.x +" "+this.y+" "+this.width+" "+this.height); | |
}, | |
calcu: function(num){ | |
console.log(this.x+this.y+this.width+this.height+num); | |
} | |
} | |
var rec = new Rectangle(0, 0, 45, 45); | |
rec.print_me(); //0 0 45 45 | |
//[1] | |
rec.calcu.call(rec, 3); //93 | |
//[2] | |
rec.calcu.apply(rec,[5]); //95 | |
//[3] | |
var fn = rec.calcu.bind(rec,7); | |
fn(); //97 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment