Function
_.after
Note this is an alternative implementation
Creates a version of the function that will only be run after first being called count times. Useful for grouping asynchronous responses, where you want to be sure that all the async calls have finished, before proceeding.
- var notes = ['profile', 'settings']
- // Underscore/Lodash
- var renderNotes = _.after(notes.length, render)
- notes.forEach(function (note) {
- console.log(note)
- renderNotes()
- })
- // Native
- notes.forEach(function (note, index) {
- console.log(note)
- if (notes.length === (index + 1)) {
- render()
- }
- })
Browser Support for Array.prototype.forEach()
✔ | ✔ | 1.5 ✔ | 9.0 ✔ | ✔ | ✔ |
_.bind
Create a new function that calls func with thisArg and args.
- var objA = {
- x: 66,
- offsetX: function(offset) {
- return this.x + offset;
- }
- }
- var objB = {
- x: 67
- };
- // Underscore/Lodash
- var boundOffsetX = _.bind(objA.offsetX, objB, 0);
- // Native
- var boundOffsetX = objA.offsetX.bind(objB, 0);
Browser Support for Function.prototype.bind()
7.0 ✔ | ✔ | 4.0 ✔ | 9.0 ✔ | 11.6 ✔ | 5.1 ✔ |
_.partial
Create a new function that calls func with args.
- // Lodash
- function greet(greeting, name) {
- return greeting + ' ' + name;
- }
- var sayHelloTo = _.partial(greet, 'Hello');
- var result = sayHelloTo('Jose')
- console.log(result)
- // output: 'Hello Jose'
- // Native
- function greet(greeting, name) {
- return greeting + ' ' + name;
- }
- var sayHelloTo = (...args) => greet('Hello', ...args)
- var result = sayHelloTo('Jose')
- console.log(result)
- // output: 'Hello Jose'
Browser Support for Spread
46.0 ✔ | 12.0 ✔ | 16.0 ✔ | ✖ | 37.0 ✔ | 8.0 ✔ |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .