Callback trong JavaScript | Callback functions phần 2

preview_player
Показать описание

Trong video này mình chia sẻ thêm về cách hoạt động của Array map() method. Điểu này giúp các bạn:
1. Hiểu thêm về cách sử dụng callback
2. Hiểu về cách hoạt động của map() method
3. Hiểu cách giải bài toán trong video mà không sử dụng map() method

Trong ví dụ này mình sử dụng for loop, điều này nhằm giúp đơn giản hóa ví dụ. Trong những videos tiếp theo các bạn sẽ nắm được chúng ta nên sử dụng for in loop trong trường hợp này để có kết quả giống với map() method của Array hơn.

Callback trong JavaScript | Callback functions phần 2

#hoclaptrinh #hoclaptrinhmienphi #javascript #frontend #backend #devops #f8
----
☻ Phần mềm sử dụng trong video:
--------------
☻ Khóa học lập trình web MIỄN PHÍ:
-------------------------------------

F8 Official
(c) Sơn Đặng

© Bản quyền thuộc về Channel F8 Official ☞ Do not Reup
© Nghiêm cấm sử dụng video nhằm mục đích thương mại dưới mọi hình thức.
Рекомендации по теме
Комментарии
Автор

ĐÃ XEM ĐƯỢC 1 NỬA KHÓA VÀ THẤY THỰC SỰ RẤT ĐÁNG BỎ THỜI GIAN XEM :D TKS A

vanonginh
Автор

Tuyệt vời. Cách của a là xem clip của em xong. Sau đó a code lại tầm 10 lần thì đã thấm rồi

huuquocdattran
Автор

Cảm ơn anh Sơn rất rất nhiều. 1 người dạy rất có tâm. Em sẽ cố gắng học hết những gì anh dạy.
Một lần nữa, cảm ơn anh Sơn!

qaqeqe
Автор

học cả list xong vẫn phải quay lại bài này xem lại a à, càng xem lại nhiều càng hiểu thêm nhiều cái khác hơn, lạ ghê :)))

phamvanhieu
Автор

mò gần 30p mới ra được cái này, mừng hú hồn lun 🤣
- filter

var a=[], length=this.length;
for(let i=0;i<length;i++){
var hi=callback(this[i]);
if(hi==true){
a.push(this[i]);
}
}
return a;
}
var courses=[1, 2, 5, 3, 6, 7, 8];

var
return a===5;
})
console.log(h);

- reduce


var length=this.length;
var h=0;
for(var i=0;i<length;i++){
h+=callback(0, this[i]);

}
return h;
}

var arr=[1, 6, 4, 7, 8, 5, 4, 10];

var tong=arr.reduce2(function(hientai, value){
return hientai+value;
}, 0)
console.log(tong);

eem
Автор

Array.prototype.reduce1 = function(callback, InitialValue)
{
var arrayLength = this.length;
for (var i = 0; i < arrayLength; ++i)
{
var previousValue = callback(InitialValue, this[i], i);
InitialValue = previousValue;
}
return previousValue;

}
function sumHandler(previousValue, currentValue, currentIndex)
{
return previousValue + currentValue;
}
let Sum = NumberArray.reduce1(sumHandler, 0);

console.log(Sum);

thiennguyenthe
Автор

var numbers = [1, 3, 5, 7, 9, 11]
Array.prototype.reduce2 = function(callback){
var total = 0
var length = numbers.length

for (var i = 0; i < length; i++){
var result = callback(this[i], i)
total += result
}

return total
}

var totalArray =
return number
})

console.log(totalArray)

lifetv-vuahocvuachoi
Автор

Tự ngẫm:
1. Định nghĩa hàm mới, có truyền tham số là 1 callback function
2. Định nghĩa callback, cần trả về giá trị mong muốn
3. Quay lại hàm định nghĩa, gọi callback để lấy giá trị mong muốn và xử lý giá trị đó để được kết quả mong muốn cuối cùng

mymy
Автор

Ôi nó khó mà càng khó lại càng cuốn anh ơiiii

Owen-wszc
Автор

a ơi cho e hoi là cái lí thuyết e thấy khá giống đệ qui nma e không chc a giúp e vs ạ

ng.quoctiennt
Автор

Cuối cùng cũng hiểu sau khi mình thử ko có vòng lặp thì nó dễ hiểu hơn xíu .
Array.prototype.map2 = function(callback){
callback(1, this[1])
}
var course = ['Java', 'Php', 'node']


course.map2(function(course, index){
console.log(course, index)
})
kết quả ra là 1 Php :))))

cathome
Автор

forEach Array.prototype.forEach2 = function(callback) {
var output = [], arrayLength = this.length;
for (var i = 0; i < arrayLength; i++) {
var result = callback(this[i], i);
output.push(result);
}
return output;
}


var names = [
'Pham A Hoang',
'Le Thi Nhan',
'Pham Van Minh'
];

var htmls = names.forEach2(function(name) {
return `<h2>${name}</h2>`;
});
console.log(htmls.join(''));

hoangpham
Автор

Đây Là incledes cho ai cần
<script>
string='Nha Trang thanh pho du lich';

array=[1, 6, 7, 4];
Array.prototype.inclues2=function(val, pos=0){
arrLength=this.length;
flaseOutput=false;
for(var i=pos; i<arrLength;i++){
if(this[i]==val){
flaseOutput= true;
break
}
}
return flaseOutput;
}
String.prototype.inclues2=function(val, pos=0){

valLenght= val.length
strLength=this.length;
flaseOutput=false;
for(var i=pos; i<strLength;i++){
if(valLenght+i>strLength){
break;
}
if(val==this.slice(i, valLenght+i)){
flaseOutput= true;
break
}
}
return flaseOutput;
}
flagResult= string.inclues2('Nha Trang', 1);
flagRedsult= array.inclues2(4);
console.log(flagResult);
</script>

hungpham-mjnl
Автор

Array.prototype.reduce1 = function(fcallback) {
var length = this.length;
var init = 0;
for(var i =0 ; i < length; i++) {
var result = fcallback(init, this[i]);
init = result
}

return init;
}


var resultCallback = courses.reduce1(function (total, value) {
return total + value;
})

console.log(resultCallback)

duyho
Автор

var array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];


Array.prototype.map2 = function(callback) {
length = this.length;
var add = 0;
for(i = 0 ; i < length ; i++) {
add += callback(this[i]);
}
return add;
}
var total = array1.map2(function(tong) {
return tong;
});

console.log(total);


Array.prototype.reduce2 = function(callback) {
length = this.length;
tong = 0 ;
for(i = 0 ; i < length; i++) {
tong += callback(this[i], 0);
}
return tong;
};

var total2 = array1.reduce2(function(tong2, index) {
return tong2 + index;
}, );

console.log(total2);

Array.prototype.find2 = function(callback) {
length = this.length;
a = [];
for(i = 0 ; i < length; i++) {
b = callback(this[i]);
if(b == true) {
a.push(this[i]);
}
}
return a;
};

var result = {
return timkiem === 5;
});

console.log(result);


Array.prototype.forEach2 = function(callback) {
length = this.length;
a = [];
for(i = 0 ; i < length ; i++) {
test = callback(this[i]);
if(test == true) {
a.push(this[i]);
}
}
return a;
};

var solon =
return solonhon > 5;
})

console.log(solon);

tuanvo
Автор

Filter2.
Array.prototype.filter2 = function (callback){
var newArray = [];
var arrayLength = this.length;
for(var i=0; i<arrayLength;i++){
var result =

if(result){
console.log(this[i]);
newArray.push(this[i]);
}
}


return newArray;
}
var course =['JavaScripts', 'PHP', 'Ruby', 'JavaScripts'];

var result =

return course;
});
console.log(result);

jvbcuong
Автор

var courses = [1, 2, 3, 4, 5];
Array.prototype.reduce2 = function(callback){
var output = 0;
var arrayLength = this.length;
let sum = 0;
for(var i = 0; i < arrayLength; i++){
var output = sum += courses[i];
}
return output;
}
var total =
return `<h1>${course}</h1>`
});
console.log(total);

Cho em xin ý kiến về phần reduce em làm ạk. Có chưa tối ưu chỗ nào k ạk

thnhphong
Автор

cách giúp người mới bỏ cuộc nhanh nhất là xem bài này, sợ thật

vanhungvu
Автор

cảm ơn bạn đã chia sẻ, xem mà thấy lỳ kì vì cách tiếp cận từ sơ bộ, đến phức tạp, rồi kết thúc câu chuyện.
Cho đóng góp tí là bạn nói hơi nhanh

congnguyenthanh
Автор

từ java chuyển qua javascript khó chịu ghê luôn

quocdattranquoc
join shbcf.ru