要求: function f() { setTimeout(function () { var data = 'hello'; }, 1000) } // 调用 f ,得到内部的数据 data f(); 实现: function f(callback) { setTimeout(function () { var data = 'hello'; callback(data); }, 1000) } // 如果需要获取一个函数中异步操作的结果,则必须通过回调函数来获取 f(function (data) { console.log(data);// hello }); find 和 findIndex 方法原理: var user = [ {id: 1, name: 'bob'}, {id: 2, name: 'bob'}, {id: 3, name: 'bob'}, ]; Array.prototype.myFind = function (callback) { for (var i = 0; i < this.length; i++) { if (callback(this[i], i)) { // return i;// 返回数组对应下标 类似 findIndex return this[i];// 返回数组对应项 类似 find } } }; var ret = user.myFind(function (item) { return item.id === 2; }); console.log(ret);// {id: 2, name: "bob"}