Binding to original this in JavaScript

I have an object that is as follows:



{ except: player => ({ send :player.getSocket().broadcast.emit }) }


However this means that the this in the emit function is not the one it expects it to be (the broadcast object).



So I can do:



{ except: player => ({ send : (msg, data) => player.getSocket().broadcast.emit(msg, data) }) }


But this is ugly, especially if the arguments change.
So the alternative is:



{ except: player => ({ send : (t = player.getSocket().broadcast).emit.bind(t) }) }


But is there a tidier way of doing this, of assigning a function to an object while maintaining it's this as it's parent object.



Answers

You can specify who this will be by calling the function with apply or call. The first parameter to these methods will be this within the body of the function.



Answers

Using bind in order to maintain the scope of this is actually a good solution to your problem, the way I see it, it makes the code a lot more understandable because you can know to what object the function actually belongs, by using apply or call, when you read the code you wont know to what this the function/method belongs. As far as i know, bind is a pretty good solution to your problem here.





gulp-image-resize With Multiple Images

Is it possible to have gulp-image-resize spit out multiple images, for example a @1x and @2x image. My gulp task looks like this currently:



gulp.task('images', function() {
return gulp.src('test.jpg')
.pipe(imageResize({ width: 1920 }))
.pipe(imagemin({
progressive: true
}))
.pipe(rename({
suffix: '@2x'
}))
.pipe(gulp.dest('./_site/public/img'))
.pipe(imageResize({ width: 960 }))
.pipe(imagemin({
progressive: true
}))
.pipe(gulp.dest('./_site/public/img'))
});


But it just places an image called test@2x.jpg with a width of 960px into my destination directory.



Is there a way to have two images saved. One called test.jpg with a width of 960px, and one called test@2x.jpg with a width of 1920px?



Any help is appreciated. Thanks in advance!



Answers

You are renaming the file too early, just put the rename pipe after the first gulp.dest.



gulp.task('images', function() {
return gulp.src('test.jpg')
.pipe(imageResize({ width: 1920 }))
.pipe(imagemin({
progressive: true
}))
.pipe(gulp.dest('./_site/public/img')) // move this line to before the gulp.dest
.pipe(rename({
suffix: '@2x'
}))
.pipe(imageResize({ width: 960 }))
.pipe(imagemin({
progressive: true
}))
.pipe(gulp.dest('./_site/public/img'))
});




WPF: all plus sections disapeared

suddenly all the option all expand/collapse conrtollers in coded disapeared (the little '+' and '-' icon):





How can i bring it back ?



Answers

enable this option from Tools -> Option .... in your case goto XAML option instead of C#...you need to close and reopen file for get setting applied





↑このページのトップヘ