Skip to content

Instantly share code, notes, and snippets.

@woshichuanqilz
Created June 17, 2024 20:28
Show Gist options
  • Save woshichuanqilz/5ae1e17b408a8685f7c66bd83d0ee46a to your computer and use it in GitHub Desktop.
Save woshichuanqilz/5ae1e17b408a8685f7c66bd83d0ee46a to your computer and use it in GitHub Desktop.
// 创建一个人物对象
const person = {
name: 'John',
age: 30,
greet: function() {
console.log(`你好,我的名字是${this.name},我今年${this.age}岁。`);
}
};
// 创建一个带有绑定执行上下文的新函数
const greetFunction = person.greet.bind(person);
// 调用绑定的函数
greetFunction(); // 输出:你好,我的名字是John,我今年30岁。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment