Skip to content

Commit 6aa3bbe

Browse files
committed
feat: add functions code
1 parent 05b446f commit 6aa3bbe

8 files changed

Lines changed: 139 additions & 0 deletions

File tree

Snippets/Functions/bind.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let user = {
2+
firstName: "John",
3+
sayHi() {
4+
alert(`Hello, ${this.firstName}!`);
5+
},
6+
};
7+
8+
let sayHi = user.sayHi.bind(user); // (*)
9+
10+
// can run it without an object
11+
sayHi(); // Hello, John!
12+
13+
setTimeout(sayHi, 1000); // Hello, John!
14+
15+
// even if the value of user changes within 1 second
16+
// sayHi uses the pre-bound value which is reference to the old user object
17+
user = {
18+
sayHi() {
19+
alert("Another user in setTimeout!");
20+
},
21+
};

Snippets/Functions/bindAll.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// If an object has many methods and we plan to actively pass it around, then we could bind them all in a loop
2+
3+
const user = {
4+
func1() {
5+
console.log("func 1");
6+
},
7+
func2() {
8+
console.log("func 2");
9+
},
10+
func3() {
11+
console.log("func 3");
12+
},
13+
};
14+
15+
for (let key in user) {
16+
if (typeof user[key] == "function") {
17+
user[key] = user[key].bind(user);
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
function f() {
3+
console.log(this); // null
4+
}
5+
6+
let user = {
7+
g: f.bind(null),
8+
};
9+
10+
// f();
11+
user.g();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function askPassword(ok, fail) {
2+
let password = "rockstar";
3+
if (password == "rockstar") ok();
4+
else fail();
5+
}
6+
7+
let user = {
8+
name: "John",
9+
10+
loginOk() {
11+
console.log(`${this.name} logged in`);
12+
},
13+
14+
loginFail() {
15+
console.log(`${this.name} failed to log in`);
16+
},
17+
};
18+
19+
askPassword(user.loginOk.bind(user), user.loginFail.bind(user));
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function partial(func, ...argsBound) {
2+
return function (...args) {
3+
// (*)
4+
return func.call(this, ...argsBound, ...args);
5+
};
6+
}
7+
8+
// Usage:
9+
let user = {
10+
firstName: "John",
11+
say(time, phrase) {
12+
alert(`[${time}] ${this.firstName}: ${phrase}!`);
13+
},
14+
};
15+
16+
// add a partial method with fixed time
17+
user.sayNow = partial(
18+
user.say,
19+
new Date().getHours() + ":" + new Date().getMinutes()
20+
);
21+
22+
user.sayNow("Hello");
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function mul(a, b) {
2+
return a * b;
3+
}
4+
5+
const triple = mul.bind(null, 3); // alternative to this is to curry functions
6+
7+
console.log(triple(4));
8+
9+
const user = {
10+
firstName: "Samyak Shah",
11+
};
12+
function send(from, text, to) {
13+
return `
14+
By: ${from}
15+
To: ${to}
16+
Message:
17+
${text}, ${this.firstName}`;
18+
}
19+
20+
const sendTo = send.bind(user, "[email protected]");
21+
22+
console.log(sendTo("Hello World", "[email protected]"));

Snippets/Functions/partialLogin.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function askPassword(ok, fail) {
2+
let password = prompt("Password?", "");
3+
if (password == "rockstar") ok();
4+
else fail();
5+
}
6+
7+
let user = {
8+
name: "John",
9+
10+
login(result) {
11+
alert(this.name + (result ? " logged in" : " failed to log in"));
12+
},
13+
};
14+
15+
askPassword(user.login.bind(user, true), user.login.bind(user, false)); // ?

Snippets/Functions/secondBind.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// A function cannot be re-bound
2+
function f() {
3+
console.log(this.name);
4+
}
5+
6+
f = f.bind({ name: "John" });
7+
z = f.bind({ name: "Ann" });
8+
9+
f(); // John
10+
z(); // John

0 commit comments

Comments
 (0)